Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGeoScaledShape.cxx
Go to the documentation of this file.
1 // @(#)root/geom:$Id$
2 // Author: Andrei Gheata 26/09/05
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 "TGeoManager.h"
16 #include "TGeoMatrix.h"
17 #include "TGeoVolume.h"
18 #include "TGeoNode.h"
19 #include "TGeoScaledShape.h"
20 #include "TBuffer3D.h"
21 #include "TBuffer3DTypes.h"
22 #include "TMath.h"
23 
24 /** \class TGeoScaledShape
25 \ingroup Geometry_classes
26 
27 A shape scaled by a TGeoScale transformation
28 \image html geom_scaledshape.png
29 */
30 
31 ClassImp(TGeoScaledShape);
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Default constructor
35 
36 TGeoScaledShape::TGeoScaledShape()
37 {
38  fShape = 0;
39  fScale = 0;
40 }
41 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Constructor
45 
46 TGeoScaledShape::TGeoScaledShape(const char *name, TGeoShape *shape, TGeoScale *scale)
47  :TGeoBBox(name,0,0,0)
48 {
49  fShape = shape;
50  fScale = scale;
51  if (!fScale->IsRegistered()) fScale->RegisterYourself();
52  ComputeBBox();
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Constructor
57 
58 TGeoScaledShape::TGeoScaledShape(TGeoShape *shape, TGeoScale *scale)
59 {
60  fShape = shape;
61  fScale = scale;
62  if (!fScale->IsRegistered()) fScale->RegisterYourself();
63  ComputeBBox();
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// destructor
68 
69 TGeoScaledShape::~TGeoScaledShape()
70 {
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Computes capacity of this shape [length^3]
75 
76 Double_t TGeoScaledShape::Capacity() const
77 {
78  Double_t capacity = fShape->Capacity();
79  const Double_t *scale = fScale->GetScale();
80  capacity *= scale[0]*scale[1]*scale[2];
81  return capacity;
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Compute bounding box of the scaled shape
86 
87 void TGeoScaledShape::ComputeBBox()
88 {
89  if (!fShape) {
90  Error("ComputeBBox", "Scaled shape %s without shape", GetName());
91  return;
92  }
93  if (fShape->IsAssembly()) fShape->ComputeBBox();
94  TGeoBBox *box = (TGeoBBox*)fShape;
95  const Double_t *orig = box->GetOrigin();
96  Double_t point[3], master[3];
97  point[0] = box->GetDX();
98  point[1] = box->GetDY();
99  point[2] = box->GetDZ();
100 
101  fScale->LocalToMaster(orig, fOrigin);
102  fScale->LocalToMaster(point, master);
103  fDX = TMath::Abs(master[0]);
104  fDY = TMath::Abs(master[1]);
105  fDZ = TMath::Abs(master[2]);
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Compute normal to closest surface from POINT.
110 
111 void TGeoScaledShape::ComputeNormal(const Double_t *point, const Double_t *dir, Double_t *norm)
112 {
113  Double_t local[3], ldir[3], lnorm[3];
114  fScale->MasterToLocal(point,local);
115  fScale->MasterToLocalVect(dir,ldir);
116  TGeoMatrix::Normalize(ldir);
117  fShape->ComputeNormal(local,ldir,lnorm);
118 // fScale->LocalToMasterVect(lnorm, norm);
119  fScale->MasterToLocalVect(lnorm, norm);
120  TGeoMatrix::Normalize(norm);
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Test if point is inside the scaled shape
125 
126 Bool_t TGeoScaledShape::Contains(const Double_t *point) const
127 {
128  Double_t local[3];
129  fScale->MasterToLocal(point,local);
130  return fShape->Contains(local);
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// compute closest distance from point px,py to each vertex. Should not be called.
135 
136 Int_t TGeoScaledShape::DistancetoPrimitive(Int_t px, Int_t py)
137 {
138  Int_t n = fShape->GetNmeshVertices();
139  return ShapeDistancetoPrimitive(n, px, py);
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// Compute distance from inside point to surface of the scaled shape.
144 
145 Double_t TGeoScaledShape::DistFromInside(const Double_t *point, const Double_t *dir, Int_t iact, Double_t step, Double_t *safe) const
146 {
147  Double_t local[3], ldir[3];
148  Double_t lstep;
149  fScale->MasterToLocal(point,local);
150  lstep = fScale->MasterToLocal(step, dir);
151  fScale->MasterToLocalVect(dir,ldir);
152  TGeoMatrix::Normalize(ldir);
153  Double_t dist = fShape->DistFromInside(local,ldir, iact, lstep, safe);
154  if (iact<3 && safe) *safe = fScale->LocalToMaster(*safe);
155  dist = fScale->LocalToMaster(dist, ldir);
156  return dist;
157 }
158 
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Compute distance from outside point to surface of the scaled shape.
162 
163 Double_t TGeoScaledShape::DistFromOutside(const Double_t *point, const Double_t *dir, Int_t iact, Double_t step, Double_t *safe) const
164 {
165  Double_t local[3], ldir[3];
166  Double_t lstep;
167 // printf("DistFromOutside(%f,%f,%f, %f,%f,%f)\n", point[0], point[1], point[2], dir[0], dir[1],dir[2]);
168  fScale->MasterToLocal(point,local);
169 // printf("local: %f,%f,%f\n", local[0],local[1], local[2]);
170  lstep = fScale->MasterToLocal(step, dir);
171  fScale->MasterToLocalVect(dir,ldir);
172  TGeoMatrix::Normalize(ldir);
173 // printf("localdir: %f,%f,%f\n",ldir[0],ldir[1],ldir[2]);
174  Double_t dist = fShape->DistFromOutside(local,ldir, iact, lstep, safe);
175 // printf("local distance: %f\n", dist);
176  if (safe) *safe = fScale->LocalToMaster(*safe);
177  dist = fScale->LocalToMaster(dist, ldir);
178 // printf("converted distance: %f\n",dist);
179  return dist;
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Cannot divide assemblies.
184 
185 TGeoVolume *TGeoScaledShape::Divide(TGeoVolume * /*voldiv*/, const char *divname, Int_t /*iaxis*/, Int_t /*ndiv*/,
186  Double_t /*start*/, Double_t /*step*/)
187 {
188  Error("Divide", "Scaled shapes cannot be divided. Division volume %s not created", divname);
189  return 0;
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Fills a static 3D buffer and returns a reference.
194 
195 const TBuffer3D & TGeoScaledShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
196 {
197  TBuffer3D &buffer = (TBuffer3D &)fShape->GetBuffer3D(reqSections, localFrame);
198 
199 // TGeoBBox::FillBuffer3D(buffer, reqSections, localFrame);
200  Double_t halfLengths[3] = { fDX, fDY, fDZ };
201  buffer.SetAABoundingBox(fOrigin, halfLengths);
202  if (!buffer.fLocalFrame) {
203  TransformPoints(buffer.fBBVertex[0], 8);
204  }
205 
206  if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
207  SetPoints(buffer.fPnts);
208  if (!buffer.fLocalFrame) {
209  TransformPoints(buffer.fPnts, buffer.NbPnts());
210  }
211  }
212 
213  return buffer;
214 }
215 ////////////////////////////////////////////////////////////////////////////////
216 /// in case shape has some negative parameters, these has to be computed
217 /// in order to fit the mother
218 
219 TGeoShape *TGeoScaledShape::GetMakeRuntimeShape(TGeoShape * /*mother*/, TGeoMatrix * /*mat*/) const
220 {
221  Error("GetMakeRuntimeShape", "Scaled shapes cannot be parametrized.");
222  return NULL;
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Returns numbers of vertices, segments and polygons composing the shape mesh.
227 
228 void TGeoScaledShape::GetMeshNumbers(Int_t &nvert, Int_t &nsegs, Int_t &npols) const
229 {
230  fShape->GetMeshNumbers(nvert, nsegs, npols);
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// print shape parameters
235 
236 void TGeoScaledShape::InspectShape() const
237 {
238  printf("*** Shape %s: TGeoScaledShape ***\n", GetName());
239  fScale->Print();
240  fShape->InspectShape();
241  TGeoBBox::InspectShape();
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// Returns true if the scaled shape is an assembly.
246 
247 Bool_t TGeoScaledShape::IsAssembly() const
248 {
249  return fShape->IsAssembly();
250 }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Check if the scale transformation is a reflection.
254 
255 Bool_t TGeoScaledShape::IsReflected() const
256 {
257  return fScale->IsReflection();
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Creates a TBuffer3D describing *this* shape.
262 /// Coordinates are in local reference frame.
263 
264 TBuffer3D *TGeoScaledShape::MakeBuffer3D() const
265 {
266  TBuffer3D *buff = fShape->MakeBuffer3D();
267  if (buff) SetPoints(buff->fPnts);
268  return buff;
269 }
270 
271 ////////////////////////////////////////////////////////////////////////////////
272 /// Create a scaled shape starting from a non-scaled one.
273 
274 TGeoShape *TGeoScaledShape::MakeScaledShape(const char *name, TGeoShape *shape, TGeoScale *scale)
275 {
276  TGeoShape *new_shape;
277  if (shape->IsA() == TGeoScaledShape::Class()) {
278  TGeoScaledShape *sshape = (TGeoScaledShape*)shape;
279  TGeoScale *old_scale = sshape->GetScale();
280  TGeoShape *old_shape = sshape->GetShape();
281  scale->SetScale(scale->GetScale()[0]*old_scale->GetScale()[0],
282  scale->GetScale()[1]*old_scale->GetScale()[1],
283  scale->GetScale()[2]*old_scale->GetScale()[2]);
284  new_shape = new TGeoScaledShape(name, old_shape, scale);
285  return new_shape;
286  }
287  new_shape = new TGeoScaledShape(name, shape, scale);
288  return new_shape;
289 }
290 
291 ////////////////////////////////////////////////////////////////////////////////
292 /// Fill TBuffer3D structure for segments and polygons.
293 
294 void TGeoScaledShape::SetSegsAndPols(TBuffer3D &buff) const
295 {
296  fShape->SetSegsAndPols(buff);
297 }
298 
299 ////////////////////////////////////////////////////////////////////////////////
300 /// computes the closest distance from given point to this shape, according
301 /// to option. The matching point on the shape is stored in spoint.
302 
303 Double_t TGeoScaledShape::Safety(const Double_t *point, Bool_t in) const
304 {
305  Double_t local[3];
306  fScale->MasterToLocal(point,local);
307  Double_t safe = fShape->Safety(local,in);
308  safe = fScale->LocalToMaster(safe);
309  return safe;
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// Save a primitive as a C++ statement(s) on output stream "out".
314 
315 void TGeoScaledShape::SavePrimitive(std::ostream &out, Option_t *option)
316 {
317  if (TObject::TestBit(kGeoSavePrimitive)) return;
318  out << " // Shape: " << GetName() << " type: " << ClassName() << std::endl;
319  if (!fShape || !fScale) {
320  out << "##### Invalid shape or scale !. Aborting. #####" << std::endl;
321  return;
322  }
323  fShape->SavePrimitive(out, option);
324  TString sname = fShape->GetPointerName();
325  const Double_t *sc = fScale->GetScale();
326  out << " // Scale factor:" << std::endl;
327  out << " TGeoScale *pScale = new TGeoScale(\"" << fScale->GetName()
328  << "\"," << sc[0] << "," << sc[1] << "," << sc[2] << ");" << std::endl;
329  out << " TGeoScaledShape *" << GetPointerName() << " = new TGeoScaledShape(\""
330  << GetName() << "\"," << sname << ", pScale);" << std::endl;
331 }
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /// Mesh points for scaled shapes.
335 
336 void TGeoScaledShape::SetPoints(Double_t *points) const
337 {
338  Int_t npts = fShape->GetNmeshVertices();
339  fShape->SetPoints(points);
340  Double_t master[3];
341  for (Int_t i=0; i<npts; i++) {
342  fScale->LocalToMaster(&points[3*i], master);
343  memcpy(&points[3*i], master, 3*sizeof(Double_t));
344  }
345 }
346 
347 ////////////////////////////////////////////////////////////////////////////////
348 /// Mesh points for scaled shapes.
349 
350 void TGeoScaledShape::SetPoints(Float_t *points) const
351 {
352  Int_t npts = fShape->GetNmeshVertices();
353  fShape->SetPoints(points);
354  Double_t master[3];
355  Double_t local[3];
356  Int_t index;
357  for (Int_t i=0; i<npts; i++) {
358  index = 3*i;
359  local[0] = points[index];
360  local[1] = points[index+1];
361  local[2] = points[index+2];
362  fScale->LocalToMaster(local, master);
363  points[index] = master[0];
364  points[index+1] = master[1];
365  points[index+2] = master[2];
366  }
367 }
368 
369 ////////////////////////////////////////////////////////////////////////////////
370 /// Check the inside status for each of the points in the array.
371 /// Input: Array of point coordinates + vector size
372 /// Output: Array of Booleans for the inside of each point
373 
374 void TGeoScaledShape::Contains_v(const Double_t *points, Bool_t *inside, Int_t vecsize) const
375 {
376  for (Int_t i=0; i<vecsize; i++) inside[i] = Contains(&points[3*i]);
377 }
378 
379 ////////////////////////////////////////////////////////////////////////////////
380 /// Compute the normal for an array o points so that norm.dot.dir is positive
381 /// Input: Arrays of point coordinates and directions + vector size
382 /// Output: Array of normal directions
383 
384 void TGeoScaledShape::ComputeNormal_v(const Double_t *points, const Double_t *dirs, Double_t *norms, Int_t vecsize)
385 {
386  for (Int_t i=0; i<vecsize; i++) ComputeNormal(&points[3*i], &dirs[3*i], &norms[3*i]);
387 }
388 
389 ////////////////////////////////////////////////////////////////////////////////
390 /// Compute distance from array of input points having directions specified by dirs. Store output in dists
391 
392 void TGeoScaledShape::DistFromInside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step) const
393 {
394  for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromInside(&points[3*i], &dirs[3*i], 3, step[i]);
395 }
396 
397 ////////////////////////////////////////////////////////////////////////////////
398 /// Compute distance from array of input points having directions specified by dirs. Store output in dists
399 
400 void TGeoScaledShape::DistFromOutside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step) const
401 {
402  for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromOutside(&points[3*i], &dirs[3*i], 3, step[i]);
403 }
404 
405 ////////////////////////////////////////////////////////////////////////////////
406 /// Compute safe distance from each of the points in the input array.
407 /// Input: Array of point coordinates, array of statuses for these points, size of the arrays
408 /// Output: Safety values
409 
410 void TGeoScaledShape::Safety_v(const Double_t *points, const Bool_t *inside, Double_t *safe, Int_t vecsize) const
411 {
412  for (Int_t i=0; i<vecsize; i++) safe[i] = Safety(&points[3*i], inside[i]);
413 }