Index: trunk/src/geometry_calib.m
===================================================================
--- trunk/src/geometry_calib.m	(revision 1110)
+++ trunk/src/geometry_calib.m	(revision 1111)
@@ -255,9 +255,14 @@
     ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels'];
     [num2str(numel(ind_removed)) ' points removed']});
-SliceCoord_ref=[0 0 0]; %default reference plane
+% SliceCoord_ref=[0 0 0]; %default reference plane
 if strcmp(answer,'Yes') %store the calibration data
-    if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration
-        answer=msgbox_uvmat('INPUT_Y-N',{['Assume that the current image is in the plane of the calib points z=' num2str(Z_plane) ] ; 'can be later modified by MenuSetSlice in the upper bar menu of uvmat'});
-        SliceCoord_ref=Z_plane'*[0 0 1];
+    Z=Coord(:,3);
+    if strcmp(calib_cell{val}(1:2),'3D') && isequal(max(Z),min(Z))%set the plane position for 3D (projection) calibration
+        %set the Z position of the reference plane used for calibration
+        answer=msgbox_uvmat('INPUT_Y-N',{['Assume that the illuminated plane is at z=' num2str(Z(1)) ] ; 'can be later modified by MenuSetSlice in the upper bar menu of uvmat'});
+        if strcmp(answer,'Yes')
+            GeometryCalib.NbSlice=1;
+            GeometryCalib.SliceCoord=[0 0 Z(1)];
+        end
     end
 else
@@ -390,6 +395,8 @@
 ind_max=[];ind_removed=[];Z_plane=[];
 % apply the calibration, whose type is selected in  handles.calib_type
+CoordFlip=Coord;
+CoordFlip(:,3)=-Coord(:,3);
 if ~isempty(Coord)
-    GeometryCalib=feval(CalibFcn,Coord,Intrinsic);
+    GeometryCalib=feval(CalibFcn,CoordFlip,Intrinsic);
 else
     msgbox_uvmat('ERROR','No calibration points, abort')
@@ -401,10 +408,10 @@
 
 % estimate calibration error rms and max
-X=Coord(:,1);
-Y=Coord(:,2);
+% X=Coord(:,1);
+% Y=Coord(:,2);
 Z=Coord(:,3);
 x_ima=Coord(:,4);
 y_ima=Coord(:,5);
-[Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
+[Xpoints,Ypoints]=px_XYZ(GeometryCalib,Coord(:,1),Coord(:,2),Coord(:,3));% convention of downward z coordinate (facing the camera)
 GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
 GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
@@ -434,10 +441,10 @@
 end
 GeometryCalib.ErrorMax=ErrorMax;
-%set the Z position of the reference plane used for calibration
-if isequal(max(Z),min(Z))%Z constant
-    Z_plane=Z(1);
-    GeometryCalib.NbSlice=1;
-    GeometryCalib.SliceCoord=[0 0 Z_plane];
-end
+% %set the Z position of the reference plane used for calibration
+% if isequal(max(Z),min(Z))%Z constant
+%     Z_plane=Z(1);
+%     GeometryCalib.NbSlice=1;
+%     GeometryCalib.SliceCoord=[0 0 Z_plane];
+% end
 
 
Index: trunk/src/phys_XYZ.m
===================================================================
--- trunk/src/phys_XYZ.m	(revision 1110)
+++ trunk/src/phys_XYZ.m	(revision 1111)
@@ -76,4 +76,7 @@
 if isfield(Calib,'R')
     R=(Calib.R)';
+    R(3)=-R(3);
+    R(6)=-R(6);
+    R(9)=-R(9);
     c=Z0virt;
     cvirt=Z0virt;
@@ -91,5 +94,5 @@
         cvirt=Z0virt-avirt*Calib.SliceCoord(Zindex,1)-bvirt*Calib.SliceCoord(Zindex,2);% Z0 = (virtual) z coordinate on the rotation axis (assumed horizontal)
                                % c=z coordinate at (x,y)=(0,0)
-        c=Z0virt-a*Calib.SliceCoord(Zindex,1)-b*Calib.SliceCoord(Zindex,2);
+        c=Z0-a*Calib.SliceCoord(Zindex,1)-b*Calib.SliceCoord(Zindex,2);
         R(1)=R(1)+avirt*R(3);
         R(2)=R(2)+bvirt*R(3);
Index: trunk/src/px_XYZ.m
===================================================================
--- trunk/src/px_XYZ.m	(revision 1110)
+++ trunk/src/px_XYZ.m	(revision 1111)
@@ -47,5 +47,6 @@
         H=Calib.InterfaceCoord(3);
         if H>Zphys
-            Zphys=H-(H-Zphys)/Calib.RefractionIndex; %corrected z (virtual object)
+            Zphys=H-(H-Zphys)/Calib.RefractionIndex; %corrected z (virtual object)Calib
+            
           %  test_refraction=1;
         end
@@ -53,4 +54,5 @@
     
     %camera coordinates
+    Zphys=-Zphys;%flip z coordinates
     xc=R(1)*Xphys+R(2)*Yphys+R(3)*Zphys+Calib.Tx_Ty_Tz(1);
     yc=R(4)*Xphys+R(5)*Yphys+R(6)*Zphys+Calib.Tx_Ty_Tz(2);
