StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TVolumePosition.cxx
1 // @(#)root/table:$Id$
2 // Author: Valery Fine(fine@bnl.gov) 25/12/98
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 
13 #include "Riostream.h"
14 
15 #include "TCernLib.h"
16 #include "TVolumePosition.h"
17 #include "TVolume.h"
18 
19 #include "TROOT.h"
20 #include "TClass.h"
21 #include "TVirtualPad.h"
22 #include "TGeometry.h"
23 #include "TRotMatrix.h"
24 #include "TBrowser.h"
25 #include "X3DBuffer.h"
26 
27 #include "TTablePadView3D.h"
28 
29 //R__EXTERN Size3D gSize3D;
30 
31 ClassImp(TVolumePosition);
32 
33 //______________________________________________________________________________
34 //*-*-*-*-*-*-*-*-* S T N O D E P O S I T I O N description *-*-*-*-*-*-*-*-*-
35 //*-* ===========================
36 //*-*
37 //*-* A TVolumePosition object is used to build the geometry hierarchy (see TGeometry).
38 //*-* A node may contain other nodes.
39 //*-*
40 //*-* A geometry node has attributes:
41 //*-* - name and title
42 //*-* - pointer to the referenced shape (see TShape).
43 //*-* - x,y,z offset with respect to the mother node.
44 //*-* - pointer to the rotation matrix (see TRotMatrix).
45 //*-*
46 //*-* A node can be drawn.
47 //*-*
48 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
49 
50 
60 
61 TVolumePosition::TVolumePosition(TVolume *node,Double_t x, Double_t y, Double_t z, const char *matrixname)
62 : fMatrix(0),fNode(node),fId(0)
63 {
64  SetMatrixOwner(kFALSE);
65  fX[0] = x; fX[1] =y; fX[2] = z;
66  if (!node) return;
67  static Int_t counter = 0;
68  counter++;
69  if(!(counter%1000))std::cout<<"TVolumePosition count="<<counter<<" name="<<node->GetName()<<std::endl;
70 
71  if (!gGeometry) new TGeometry;
72  if (matrixname && strlen(matrixname)) fMatrix = gGeometry->GetRotMatrix(matrixname);
73  if (!fMatrix) fMatrix = TVolume::GetIdentity();
74 }
75 
76 
86 
87 TVolumePosition::TVolumePosition(TVolume *node,Double_t x, Double_t y, Double_t z, TRotMatrix *matrix)
88  : fMatrix(matrix),fNode(node),fId(0)
89 {
90  SetMatrixOwner(kFALSE);
91  if (!gGeometry) new TGeometry;
92  fX[0] = x; fX[1] = y; fX[2] = z;
93  if (!fMatrix) fMatrix = TVolume::GetIdentity();
94 }
97 
98 TVolumePosition::TVolumePosition(const TVolumePosition* oldPosition, const TVolumePosition* curPosition){
99  fMatrix = 0;
100  SetMatrixOwner(kFALSE);
101  TVolume *curNode = 0;
102  UInt_t curPositionId = 0;
103  TRotMatrix *curMatrix = 0;
104  if (curPosition) {
105  curNode = curPosition->GetNode();
106  curPositionId = curPosition->GetId();
107  curMatrix = (TRotMatrix *) curPosition->GetMatrix();
108  }
109  TRotMatrix *oldMatrix = 0;
110  fX[0] = 0; fX[1] = 0; fX[2] = 0;
111  Double_t oldTranslation[] = { 0, 0, 0 };
112  if (oldPosition) {
113  oldMatrix = (TRotMatrix *) oldPosition->GetMatrix();
114  oldTranslation[0] = oldPosition->GetX();
115  oldTranslation[1] = oldPosition->GetY();
116  oldTranslation[2] = oldPosition->GetZ();
117  }
118 
119  // Pick the "current" position by pieces
120 
121  // Create a new position
122  Double_t newMatrix[9];
123 
124  if(oldMatrix && curMatrix && curPosition) {
125  TGeometry::UpdateTempMatrix(oldTranslation,oldMatrix->GetMatrix(),
126  curPosition->GetX(),curPosition->GetY(),curPosition->GetZ(),
127  curMatrix->GetMatrix(),
128  fX,newMatrix);
129  Int_t num = gGeometry->GetListOfMatrices()->GetSize();
130  Char_t anum[100];
131  snprintf(anum,100,"%d",num+1);
132  fMatrix = new TRotMatrix(anum,"NodeView",newMatrix);
133  SetMatrixOwner(kTRUE);
134  } else {
135  if (curPosition) {
136  fX[0] = oldTranslation[0] + curPosition->GetX();
137  fX[1] = oldTranslation[1] + curPosition->GetY();
138  fX[2] = oldTranslation[2] + curPosition->GetZ();
139  fMatrix = curMatrix;
140  }
141  }
142  fId = curPositionId;
143  fNode = curNode;
144 }
145 //______________________________________________________________________________
146 //______________________________________________________________________________
148  , fMatrix(((TVolumePosition &)pos).GetMatrix()),fNode(pos.GetNode()),fId(pos.GetId())
149 {
150  //to be documented
151  for (int i=0;i<3;i++) fX[i] = pos.GetX(i);
152  // Transferring the ownership.
153  // The last created object owns the matrix if any.
154  // The source object gives up its ownership in favour of the destination object
155 
156  SetMatrixOwner(pos.IsMatrixOwner());
157  // !!! We have to break the "const'ness" at this point to take the ownerships
158  ((TVolumePosition &)pos).SetMatrixOwner(kFALSE);
159 }
160 
163 
165 {
166  DeleteOwnMatrix();
167 }
170 
171 void TVolumePosition::Browse(TBrowser *b)
172 {
173  if (GetNode()) {
174  TShape *shape = GetNode()->GetShape();
175  b->Add(GetNode(),shape?shape->GetName():GetNode()->GetName());
176  } else {
177  Draw();
178  gPad->Update();
179  }
180 }
181 
186 
188 {
189  return 99999;
190 }
191 
194 
195 void TVolumePosition::Draw(Option_t *option)
196 {
197  TVolume *node = GetNode();
198  if (node) node->Draw(option);
199 }
200 
201 
206 
207 void TVolumePosition::ExecuteEvent(Int_t, Int_t, Int_t)
208 {
209 // if (gPad->GetView())
210 // gPad->GetView()->ExecuteRotateView(event, px, py);
211 
212 // if (!gPad->GetListOfPrimitives()->FindObject(this)) gPad->SetCursor(kCross);
213  gPad->SetCursor(kHand);
214 }
215 
218 
219 const Char_t *TVolumePosition::GetName() const
220 {
221  return GetNode()?GetNode()->GetName():IsA()->GetName();
222 }
223 
226 
227 char *TVolumePosition::GetObjectInfo(Int_t, Int_t) const
228 {
229  if (!gPad) return 0;
230  if (!GetNode()) return 0;
231  static char info[64];
232  snprintf(info,64,"%s/%s, shape=%s/%s",GetNode()->GetName(),GetNode()->GetTitle(),GetNode()->GetShape()->GetName(),GetNode()->GetShape()->ClassName());
233  return info;
234 }
235 
238 
239 Double_t *TVolumePosition::Errmx2Master(const Double_t *localError, Double_t *masterError) const
240 {
241  Double_t error[6];
242  TCL::vzero(&error[1],4);
243  error[0] = localError[0]; error[2] = localError[1]; error[5] = localError[2];
244  return Cormx2Master(error, masterError);
245 }
246 
249 
250 Float_t *TVolumePosition::Errmx2Master(const Float_t *localError, Float_t *masterError) const
251 {
252  Float_t error[6];
253  TCL::vzero(&error[1],4);
254  error[0] = localError[0]; error[2] = localError[1]; error[5] = localError[2];
255  return Cormx2Master(error, masterError);
256 }
257 
260 
261 Double_t *TVolumePosition::Cormx2Master(const Double_t *localCorr, Double_t *masterCorr)const
262 {
263  Double_t *res = 0;
264  const TRotMatrix *rm = GetMatrix();
265  double *m = 0;
266  if (rm && ( m = ((TRotMatrix *)rm)->GetMatrix()) )
267  res = TCL::trasat(m,(Double_t *)localCorr,masterCorr,3,3);
268  else
269  res = TCL::ucopy(localCorr,masterCorr,6);
270  return res;
271 }
272 
275 
276 Float_t *TVolumePosition::Cormx2Master(const Float_t *localCorr, Float_t *masterCorr) const
277 {
278  Float_t *res = 0;
279  const TRotMatrix *rm = GetMatrix();
280  Double_t *m = 0;
281  if (rm && (m = ((TRotMatrix *)rm)->GetMatrix()) ) {
282  double corLocal[6], corGlobal[6];
283  TCL::ucopy(localCorr,corLocal,6);
284  TCL::trasat(m,corLocal,corGlobal,3,3);
285  res = TCL::ucopy(corGlobal,masterCorr,6);
286  } else
287  res = TCL::ucopy(localCorr,masterCorr,6);
288  return res;
289 }
292 
293 Double_t *TVolumePosition::Errmx2Local(const Double_t *masterError, Double_t *localError) const
294 {
295  Double_t error[6];
296  TCL::vzero(&error[1],4);
297  error[0] = masterError[0]; error[2] = masterError[1]; error[5] = masterError[2];
298  return Cormx2Local(error, localError);
299 }
302 
303 Float_t *TVolumePosition::Errmx2Local(const Float_t *masterError, Float_t *localError) const
304 {
305  Float_t error[6];
306  TCL::vzero(&error[1],4);
307  error[0] = masterError[0]; error[2] = masterError[1]; error[5] = masterError[2];
308  return Cormx2Local(error, localError);
309 }
312 
313 Double_t *TVolumePosition::Cormx2Local(const Double_t *localCorr, Double_t *masterCorr) const
314 {
315  Double_t *res = 0;
316  TRotMatrix *rm = (TRotMatrix *) GetMatrix();
317  double *m = 0;
318  if (rm && ( m = rm->GetMatrix()) )
319  res = TCL::tratsa(m,(Double_t *)localCorr,masterCorr,3,3);
320  else
321  res = TCL::ucopy(localCorr,masterCorr,6);
322  return res;
323 }
324 
327 
328 Float_t *TVolumePosition::Cormx2Local(const Float_t *localCorr, Float_t *masterCorr) const
329 {
330  Float_t *res = 0;
331  TRotMatrix *rm = (TRotMatrix *) GetMatrix();
332  Double_t *m = 0;
333  if (rm && (m = rm->GetMatrix()) ) {
334  double corLocal[6], corGlobal[6];
335  TCL::ucopy(localCorr,corLocal,6);
336  TCL::tratsa(m,corLocal,corGlobal,3,3);
337  res = TCL::ucopy(corGlobal,masterCorr,6);
338  }
339  else
340  res = TCL::ucopy(localCorr,masterCorr,6);
341  return res;
342 }
343 
351 
352 Double_t *TVolumePosition::Local2Master(const Double_t *local, Double_t *master, Int_t nPoints) const
353 {
354  Double_t *matrix = 0;
355  Double_t *trans = 0;
356  if (!fMatrix || fMatrix == TVolume::GetIdentity() || !(matrix = ((TRotMatrix *)fMatrix)->GetMatrix()) ) {
357  trans = master;
358  for (int i =0; i < nPoints; i++,local += 3, master += 3) TCL::vadd(local,fX,master,3);
359  } else {
360  trans = master;
361  for (int i =0; i < nPoints; i++, local += 3, master += 3) {
362  TCL::mxmpy2(matrix,local,master,3,3,1);
363  TCL::vadd(master,fX,master,3);
364  }
365  }
366  return trans;
367 }
368 
376 
377 Float_t *TVolumePosition::Local2Master(const Float_t *local, Float_t *master, Int_t nPoints) const
378 {
379  Double_t *matrix = 0;
380  Float_t *trans = 0;
381  if (!fMatrix || fMatrix == TVolume::GetIdentity() || !(matrix = ((TRotMatrix *)fMatrix)->GetMatrix()) )
382  {
383  trans = master;
384  for (int i =0; i < nPoints; i++,local += 3, master += 3) TCL::vadd(local,fX,master,3);
385  } else {
386  trans = master;
387  for (int i =0; i < nPoints; i++, local += 3, master += 3) {
388  Double_t dlocal[3]; Double_t dmaster[3];
389  TCL::ucopy(local,dlocal,3);
390  TCL::mxmpy2(matrix,dlocal,dmaster,3,3,1);
391  TCL::vadd(dmaster,fX,dmaster,3);
392  TCL::ucopy(dmaster,master,3);
393  }
394  }
395  return trans;
396 }
404 
405 Double_t *TVolumePosition::Master2Local(const Double_t *master, Double_t *local, Int_t nPoints) const
406 {
407  Double_t *matrix = 0;
408  Double_t *trans = 0;
409  if (!fMatrix || fMatrix == TVolume::GetIdentity() || !(matrix = ((TRotMatrix *)fMatrix)->GetMatrix()) ){
410  trans = local;
411  for (int i =0; i < nPoints; i++,master += 3, local += 3) TCL::vsub(master,fX,local,3);
412  } else {
413  trans = local;
414  for (int i =0; i < nPoints; i++, master += 3, local += 3) {
415  Double_t dlocal[3];
416  TCL::vsub(master,fX,dlocal,3);
417  TCL::mxmpy(matrix,dlocal,local,3,3,1);
418  }
419  }
420  return trans;
421 }
422 
430 
431 Float_t *TVolumePosition::Master2Local(const Float_t *master, Float_t *local, Int_t nPoints) const
432 {
433  Double_t *matrix = 0;
434  Float_t *trans = 0;
435  if (!fMatrix || fMatrix == TVolume::GetIdentity() || !(matrix = ((TRotMatrix *)fMatrix)->GetMatrix()) ){
436  trans = local;
437  for (int i =0; i < nPoints; i++,master += 3, local += 3) TCL::vsub(master,fX,local,3);
438  } else {
439  trans = local;
440  for (int i =0; i < nPoints; i++, master += 3, local += 3) {
441  Double_t dmaster[3]; Double_t dlocal[3];
442  TCL::ucopy(master,dmaster,3);
443  TCL::vsub(dmaster,fX,dmaster,3);
444  TCL::mxmpy(matrix,dmaster,dlocal,3,3,1);
445  TCL::ucopy(dlocal,local,3);
446  }
447  }
448  return trans;
449 }
452 
453 void TVolumePosition::Paint(Option_t *)
454 {
455  Error("Paint","Position can not be painted");
456 }
457 
460 
461 void TVolumePosition::Print(Option_t *) const
462 {
463  std::cout << *this << std::endl;
464 }
465 
473 
474 TVolumePosition *TVolumePosition::Reset(TVolume *node,Double_t x, Double_t y, Double_t z, TRotMatrix *matrix)
475 {
476 // This method has to be protected since it doesn't set properly kIsOwn bit.
477 
478  fNode = node;
479  SetPosition(x,y,z);
480  SetMatrix(matrix);
481  if (!fMatrix) fMatrix = TVolume::GetIdentity();
482  return this;
483 }
484 
487 
488 void TVolumePosition::SavePrimitive(std::ostream &, Option_t * /*= ""*/)
489 {
490 #if 0
491  out << "TVolumePosition *CreatePosition() { " << std::endl;
492  out << " TVolumePosition *myPosition = 0; " << std::endl;
493  Double_t x = GetX();
494  Double_t y = GetY();
495  Double_t z = GetZ();
496  TRotMatrix *matrix =
497  myPosition = new TVolumePosition(TVolume *node,Double_t x, Double_t y, Double_t z, const char *matrixname)
498  : fNode(node),fX(x),fY(y),fZ(z),fMatrix(0)
499 {
500 /
501  out << " return myPosition; " << std::endl;
502  out << "} " << std::endl;
503 #endif
504 
505 }
508 
510 {
511  TVolume *thisNode = GetNode();
512  if (thisNode) thisNode->SetLineAttributes();
513 }
516 
517 void TVolumePosition::SetMatrix(TRotMatrix *matrix)
518 {
519  if (matrix != fMatrix) {
520  DeleteOwnMatrix();
521  fMatrix = matrix;
522  }
523 }
526 
528 {
529  TTablePadView3D *view3D=(TTablePadView3D *)gPad->GetView3D();
530 //*-*- Update translation vector and rotation matrix for new level
531  if (gGeometry->GeomLevel() && fMatrix) {
532  gGeometry->UpdateTempMatrix(fX[0],fX[1],fX[2]
533  ,((TRotMatrix *)fMatrix)->GetMatrix()
534  ,fMatrix->IsReflection());
535  if (view3D)
536  view3D->UpdatePosition(fX[0],fX[1],fX[2],((TRotMatrix *)fMatrix));
537  }
538 }
539 
542 
544 {
545  TVolume *node = GetNode();
546  if (node) node->SetVisibility(TVolume::ENodeSEEN(vis));
547 }
549 
550 TVolumePosition &TVolumePosition::Mult(const TVolumePosition &curPosition) {
551  // This method mupltiply the position of this object to the position of the
552  // curPosition object.
553  // It doesn't change Id of either object involved.
554 
555 
556  // Pick the "old" position by pieces
557  TVolume *curNode = 0;
558  // UInt_t curPositionId = 0;
559  curNode = curPosition.GetNode();
560  // curPositionId = curPosition.GetId();
561  const TRotMatrix *oldMatrix = 0;
562  Double_t oldTranslation[] = { 0, 0, 0 };
563  oldMatrix = GetMatrix();
564  oldTranslation[0] = GetX();
565  oldTranslation[1] = GetY();
566  oldTranslation[2] = GetZ();
567 
568  // Pick the "current" position by pieces
569  const TRotMatrix *curMatrix = curPosition.GetMatrix();
570 
571  // Create a new position
572  Double_t newTranslation[3];
573  Double_t newMatrix[9];
574  if(oldMatrix){
575  TGeometry::UpdateTempMatrix(oldTranslation,((TRotMatrix *)oldMatrix)->GetMatrix()
576  ,curPosition.GetX(),curPosition.GetY(),curPosition.GetZ(),
577  ((TRotMatrix *)curMatrix)->GetMatrix()
578  ,newTranslation,newMatrix);
579  Int_t num = gGeometry->GetListOfMatrices()->GetSize();
580  Char_t anum[100];
581  snprintf(anum,100,"%d",num+1);
582  SetMatrixOwner();
583  Reset(curNode
584  ,newTranslation[0],newTranslation[1],newTranslation[2]
585  ,new TRotMatrix(anum,"NodeView",newMatrix));
586  SetMatrixOwner(kTRUE);
587  } else {
588  newTranslation[0] = oldTranslation[0] + curPosition.GetX();
589  newTranslation[1] = oldTranslation[1] + curPosition.GetY();
590  newTranslation[2] = oldTranslation[2] + curPosition.GetZ();
591  Reset(curNode,newTranslation[0],newTranslation[1],newTranslation[2]);
592  }
593 // SetId(curPositionId);
594  return *this;
595 }
596 
599 
600 void TVolumePosition::SetXYZ(Double_t *xyz)
601 {
602  if (xyz) memcpy(fX,xyz,sizeof(fX));
603  else memset(fX,0,sizeof(fX));
604 }
605 
608 
609 void TVolumePosition::Streamer(TBuffer &R__b)
610 {
611  TRotMatrix *save = fMatrix;
612  if (R__b.IsReading()) {
613  fMatrix = 0;
614  R__b.ReadClassBuffer(TVolumePosition::Class(), this);
615  if (!fMatrix) fMatrix = save;
616  } else {
617  if (save == TVolume::GetIdentity() ) fMatrix = 0;
618  R__b.WriteClassBuffer(TVolumePosition::Class(), this);
619  fMatrix = save;
620  }
621 }
624 
625 std::ostream& operator<<(std::ostream& s,const TVolumePosition &target)
626 {
627  s << " Node: ";
628  if (target.GetNode()) s << target.GetNode()->GetName() << std::endl;
629  else s << "NILL" << std::endl;
630  s << Form(" Position: x=%10.5f : y=%10.5f : z=%10.5f\n", target.GetX(), target.GetY(), target.GetZ());
631  TRotMatrix *rot = (TRotMatrix *) target.GetMatrix();
632  if (rot){
633  s << rot->IsA()->GetName() << "\t" << rot->GetName() << "\t" << rot->GetTitle() << std::endl;
634  Double_t *matrix = rot->GetMatrix();
635  Int_t i = 0;
636  for (i=0;i<3;i++) {
637  for (Int_t j=0;j<3;j++) s << Form("%10.5f:", *matrix++);
638  s << std::endl;
639  }
640  }
641  return s;
642 }
virtual void Browse(TBrowser *b)
to be documented
Definition: FJcore.h:367
virtual void Draw(Option_t *depth="3")
Draw Referenced node with current parameters.
virtual Double_t * Cormx2Master(const Double_t *localCorr, Double_t *masterCorr) const
to be documented
virtual Float_t * Errmx2Master(const Float_t *localError, Float_t *masterError) const
to be documented
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
virtual void Draw(Option_t *depth="3")
Definition: TVolume.cxx:433
virtual const char * GetTitle() const
forward the GetObjectInfo call to the decorated object
virtual void SetVisibility(ENodeSEEN vis=TVolume::kBothVisible)
Definition: TVolume.cxx:753
virtual void SetXYZ(Double_t *xyz=0)
to be documented
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
to be documented
virtual Double_t * Local2Master(const Double_t *local, Double_t *master, Int_t nPoints=1) const
virtual ~TVolumePosition()
to be documented
static TRotMatrix * GetIdentity()
Return a pointer the &quot;identity&quot; matrix.
Definition: TVolume.cxx:507
virtual void SetVisibility(Int_t vis=1)
to be documented
virtual Double_t * Cormx2Local(const Double_t *masterCorr, Double_t *localCorr) const
to be documented
virtual char * GetObjectInfo(Int_t px, Int_t py) const
to be documented
virtual void SetLineAttributes()
to be documented
virtual void SetMatrix(TRotMatrix *matrix=0)
to be documented
TVolumePosition(TVolume *node=0, Double_t x=0, Double_t y=0, Double_t z=0, TRotMatrix *matrix=0)
virtual void UpdatePosition(Option_t *option="")
to be documented
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual TVolumePosition * Reset(TVolume *node=0, Double_t x=0, Double_t y=0, Double_t z=0, TRotMatrix *matrix=0)
virtual Double_t * Master2Local(const Double_t *master, Double_t *local, Int_t nPoints=1) const
static float * trasat(const float *a, const float *s, float *r, int m, int n)
Definition: TCernLib.cxx:540
virtual Float_t * Errmx2Local(const Float_t *masterError, Float_t *localError) const
to be documented
virtual void Print(Option_t *option="") const
to be documented
static float * tratsa(const float *a, const float *s, float *r, int m, int n)
Definition: TCernLib.cxx:677
virtual void Paint(Option_t *option="")
Paint Referenced node with current parameters.
virtual const Char_t * GetName() const
return VolumePosition name