Skip to content

Commit 1cb08d8

Browse files
Merge pull request #468 from rondlh/Virtual-Camera-bugfix
Virtual camera cleanup and zoomDepth bugfix
2 parents 7ed2c9e + 147b74b commit 1cb08d8

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

src/main/java/com/neuronrobotics/bowlerstudio/threed/BowlerStudio3dEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ private void runSyncFocus(TransformNR orent, TransformNR trans, double zoom) {
18461846
}
18471847
}
18481848
BowlerStudio.runLater(() -> {
1849-
getFlyingCamera().SetOrentation(orent);
1849+
getFlyingCamera().SetOrientation(orent);
18501850
getFlyingCamera().SetPosition(trans);
18511851
});
18521852

src/main/java/com/neuronrobotics/bowlerstudio/threed/VirtualCameraMobileBase.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public VirtualCameraMobileBase addListener(ICameraChangeListener l) {
6363
return this;
6464
}
6565
public VirtualCameraMobileBase removeListener(ICameraChangeListener l) {
66-
if(listeners.contains(l))
66+
if (listeners.contains(l))
6767
listeners.remove(l);
6868
return this;
6969
}
7070
public void fireUpdate() {
7171
synchronizePositionWithOtherFlyingCamera(myGlobal);
72-
for(ICameraChangeListener c:listeners) {
72+
for (ICameraChangeListener c:listeners) {
7373
try {
7474
c.onChange(this);
7575
}catch(Throwable t) {
@@ -86,30 +86,29 @@ public void setGlobalToFiducialTransform(TransformNR defautcameraView) {
8686

8787
public void updatePositions() {
8888

89-
if(System.currentTimeMillis()-timeSinceLastUpdate>16) {
90-
timeSinceLastUpdate=System.currentTimeMillis();
91-
error=false;
89+
if (System.currentTimeMillis() - timeSinceLastUpdate > 16) {
90+
timeSinceLastUpdate = System.currentTimeMillis();
91+
error = false;
9292
TransformFactory.nrToAffine(myGlobal, camerUserPerspective);
9393
}else {
9494
// too soon
95-
error=true;
95+
error = true;
9696
}
9797
}
9898

9999
public TransformNR getFiducialToGlobalTransform() {
100100
return myGlobal;
101101
}
102+
102103
public void DrivePositionAbsolute(double x, double y, double z) {
103104
TransformNR global = getFiducialToGlobalTransform().copy()
104-
.translateX(x)
105-
.translateY(y)
106-
.translateZ(z);
105+
.translateX(x).translateY(y).translateZ(z);
107106
setGlobalToFiducialTransform(global);
108-
109107
}
108+
110109
public void DriveArc(TransformNR newPose) {
111110
TransformNR pureTrans = new TransformNR();
112-
if(move) {
111+
if (move) {
113112
pureTrans.setX(newPose.getX());
114113
pureTrans.setY(newPose.getY());
115114
pureTrans.setZ(newPose.getZ());
@@ -121,26 +120,25 @@ public void DriveArc(TransformNR newPose) {
121120
double rotationElevationRadians = newPose.getRotation().getRotationElevationRadians();
122121

123122
global.setRotation(new RotationNR(
124-
(Math.toDegrees(
125-
rotationTiltRadians + global.getRotation().getRotationTiltRadians()) % 360),
126-
(Math.toDegrees(
127-
rotationAzimuthRadians + global.getRotation().getRotationAzimuthRadians()) % 360),
128-
Math.toDegrees(
129-
rotationElevationRadians + global.getRotation().getRotationElevationRadians())));
123+
(Math.toDegrees(rotationTiltRadians + global.getRotation().getRotationTiltRadians()) % 360),
124+
(Math.toDegrees(rotationAzimuthRadians + global.getRotation().getRotationAzimuthRadians()) % 360),
125+
Math.toDegrees(rotationElevationRadians + global.getRotation().getRotationElevationRadians())));
126+
130127
// global.getRotation().setStorage(nr);
131128
//com.neuronrobotics.sdk.common.Log.error("Camera tilt="+global);
132129
// New target calculated appliaed to global offset
133130
setGlobalToFiducialTransform(global);
134131
}
132+
135133
public void SetPosition(TransformNR newPose) {
136-
if(newPose==null || !move)
134+
if ((newPose == null) || !move)
137135
return;
138136
setGlobalToFiducialTransform(newPose.copy().setRotation(getFiducialToGlobalTransform().getRotation()));
139137
}
140-
public void SetOrentation(TransformNR newPose) {
141-
if(newPose==null)
138+
public void SetOrientation(TransformNR newPose) {
139+
if (newPose == null)
142140
return;
143-
//newPose=CameraGlobalOffset.times(newPose);
141+
//newPose = CameraGlobalOffset.times(newPose);
144142
// TransformNR pureTrans = new TransformNR();
145143
//
146144
// // Auto-generated method stub
@@ -150,8 +148,8 @@ public void SetOrentation(TransformNR newPose) {
150148

151149
TransformNR global = getFiducialToGlobalTransform().copy();
152150
// use the camera global fraame elevation
153-
double rotationElevationDegrees = -newPose.getRotation().getRotationElevationDegrees()-90;
154-
double azimuth = 90-Math.toDegrees(
151+
double rotationElevationDegrees = -newPose.getRotation().getRotationElevationDegrees() - 90;
152+
double azimuth = 90 - Math.toDegrees(
155153
newPose.getRotation().getRotationAzimuthRadians() );
156154
// Apply globals to the internal camer frame
157155
global.setRotation(new RotationNR(
@@ -172,12 +170,15 @@ public double getTiltAngle() {
172170
public double getGlobalX() {
173171
return getFiducialToGlobalTransform().getX();
174172
}
173+
175174
public double getGlobalY() {
176175
return getFiducialToGlobalTransform().getY();
177176
}
177+
178178
public double getGlobalZ() {
179179
return getFiducialToGlobalTransform().getZ();
180180
}
181+
181182
public TransformNR getCamerFrame() {
182183
TransformNR offset = TransformFactory.affineToNr(getOffset());
183184
TransformNR fiducialToGlobalTransform = getFiducialToGlobalTransform();
@@ -205,19 +206,20 @@ public double getZoomDepth() {
205206
}
206207

207208
public void setZoomDepth(double zoomDepth) {
208-
if(zoomlock)
209+
if (zoomlock)
209210
throw new RuntimeException("Zoom can not be set when locked");
210-
if (zoomDepth > 2)
211-
zoomDepth = 2;
212-
if (zoomDepth < -9000)
213-
zoomDepth = -9000;
211+
212+
// Clamp zoomDepth between -9000 and -2
213+
zoomDepth = Math.max(-9000, Math.min(-2, zoomDepth));
214+
214215
this.zoomDepth = zoomDepth;
215-
if(zoomDepth<-5000)
216-
camera.setFarClip(-zoomDepth*2);
217-
else
218-
camera.setFarClip(10000);
219-
zoomAffine.setTz(getZoomDepth());
220-
fireUpdate();
216+
217+
// Dynamically adjust setFarClip to reduce Z-fighting
218+
camera.setFarClip(Math.max(1500, -zoomDepth * 2));
219+
220+
zoomAffine.setTz(zoomDepth);
221+
222+
fireUpdate();
221223
}
222224

223225
public static int getDefaultZoomDepth() {
@@ -229,20 +231,21 @@ public static Affine getOffset() {
229231
}
230232

231233
public void bind(VirtualCameraMobileBase f) {
232-
if(flyingCamera.contains(f)) {
234+
if (flyingCamera.contains(f)) {
233235
return;
234236
}
235237
this.flyingCamera .add(f);
236238
}
239+
237240
private void synchronizePositionWithOtherFlyingCamera(TransformNR n) {
238241

239-
for(VirtualCameraMobileBase cam:flyingCamera) {
242+
for (VirtualCameraMobileBase cam:flyingCamera) {
240243
RotationNR rotation = getFiducialToGlobalTransform().getRotation();
241-
if (!zoomlock && !cam.zoomlock && ((int)cam.getZoomDepth())!=((int)getZoomDepth())) {
244+
if (!zoomlock && !cam.zoomlock && ((int)cam.getZoomDepth()) != ((int)getZoomDepth())) {
242245
//com.neuronrobotics.sdk.common.Log.error(name+" Sync zoom to "+cam.name);
243246
cam.setZoomDepth(zoomDepth);
244247
}
245-
if(rotation==cam.myGlobal.getRotation())
248+
if (rotation == cam.myGlobal.getRotation())
246249
continue;
247250
//com.neuronrobotics.sdk.common.Log.error(name+" pusing update to "+cam.name);
248251
if(!cam.move || !move) {
@@ -270,5 +273,4 @@ public void lockMove() {
270273
move = false;
271274
}
272275

273-
274276
}

0 commit comments

Comments
 (0)