Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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#include <iostream>
13
14#include "TGeoManager.h"
15#include "TGeoMatrix.h"
16#include "TGeoVolume.h"
17#include "TGeoNode.h"
18#include "TGeoScaledShape.h"
19#include "TBuffer3D.h"
20#include "TBuffer3DTypes.h"
21#include "TMath.h"
22
23/** \class TGeoScaledShape
24\ingroup Geometry_classes
25
26A shape scaled by a TGeoScale transformation
27\image html geom_scaledshape.png
28*/
29
30
31////////////////////////////////////////////////////////////////////////////////
32/// Default constructor
33
40
41////////////////////////////////////////////////////////////////////////////////
42/// Constructor
43
44TGeoScaledShape::TGeoScaledShape(const char *name, TGeoShape *shape, TGeoScale *scale) : TGeoBBox(name, 0, 0, 0)
45{
47 fShape = shape;
48 fScale = scale;
49 if (!fScale->IsRegistered())
50 fScale->RegisterYourself();
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor
56
58{
60 fShape = shape;
61 fScale = scale;
62 if (!fScale->IsRegistered())
63 fScale->RegisterYourself();
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// destructor
69
71
72////////////////////////////////////////////////////////////////////////////////
73/// Computes capacity of this shape [length^3]
74
76{
77 Double_t capacity = fShape->Capacity();
78 const Double_t *scale = fScale->GetScale();
79 capacity *= scale[0] * scale[1] * scale[2];
80 return capacity;
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Compute bounding box of the scaled shape
85
87{
88 if (!fShape) {
89 Error("ComputeBBox", "Scaled shape %s without shape", GetName());
90 return;
91 }
92 if (fShape->IsAssembly())
93 fShape->ComputeBBox();
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
111void TGeoScaledShape::ComputeNormal(const Double_t *point, const Double_t *dir, Double_t *norm) const
112{
113 Double_t local[3], ldir[3], lnorm[3];
114 fScale->MasterToLocal(point, local);
115 fScale->MasterToLocalVect(dir, ldir);
117 fShape->ComputeNormal(local, ldir, lnorm);
118 // fScale->LocalToMasterVect(lnorm, norm);
119 fScale->MasterToLocalVect(lnorm, norm);
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Test if point is inside the scaled shape
125
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
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
146 Double_t *safe) const
147{
148 Double_t local[3], ldir[3];
149 Double_t lstep;
150 fScale->MasterToLocal(point, local);
151 lstep = fScale->MasterToLocal(step, dir);
152 fScale->MasterToLocalVect(dir, ldir);
154 Double_t dist = fShape->DistFromInside(local, ldir, iact, lstep, safe);
155 if (iact < 3 && safe)
156 *safe = fScale->LocalToMaster(*safe);
157 dist = fScale->LocalToMaster(dist, ldir);
158 return dist;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Compute distance from outside point to surface of the scaled shape.
163
165 Double_t *safe) const
166{
167 Double_t local[3], ldir[3];
168 Double_t lstep;
169 // printf("DistFromOutside(%f,%f,%f, %f,%f,%f)\n", point[0], point[1], point[2], dir[0], dir[1],dir[2]);
170 fScale->MasterToLocal(point, local);
171 // printf("local: %f,%f,%f\n", local[0],local[1], local[2]);
172 lstep = fScale->MasterToLocal(step, dir);
173 fScale->MasterToLocalVect(dir, ldir);
175 // printf("localdir: %f,%f,%f\n",ldir[0],ldir[1],ldir[2]);
176 Double_t dist = fShape->DistFromOutside(local, ldir, iact, lstep, safe);
177 // printf("local distance: %f\n", dist);
178 if (safe)
179 *safe = fScale->LocalToMaster(*safe);
180 dist = fScale->LocalToMaster(dist, ldir);
181 // printf("converted distance: %f\n",dist);
182 return dist;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Cannot divide assemblies.
187
188TGeoVolume *TGeoScaledShape::Divide(TGeoVolume * /*voldiv*/, const char *divname, Int_t /*iaxis*/, Int_t /*ndiv*/,
189 Double_t /*start*/, Double_t /*step*/)
190{
191 Error("Divide", "Scaled shapes cannot be divided. Division volume %s not created", divname);
192 return nullptr;
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Fills a static 3D buffer and returns a reference.
197
198const TBuffer3D &TGeoScaledShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
199{
200 TBuffer3D &buffer = (TBuffer3D &)fShape->GetBuffer3D(reqSections, localFrame);
201 buffer.fScaled = kTRUE;
202
203 // TGeoBBox::FillBuffer3D(buffer, reqSections, localFrame);
204 Double_t halfLengths[3] = {fDX, fDY, fDZ};
205 buffer.SetAABoundingBox(fOrigin, halfLengths);
206 if (!buffer.fLocalFrame) {
207 TransformPoints(buffer.fBBVertex[0], 8);
208 }
209
210 if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
211 SetPoints(buffer.fPnts);
212 if (!buffer.fLocalFrame) {
213 TransformPoints(buffer.fPnts, buffer.NbPnts());
214 }
215 }
216
217 return buffer;
218}
219////////////////////////////////////////////////////////////////////////////////
220/// in case shape has some negative parameters, these has to be computed
221/// in order to fit the mother
222
224{
225 Error("GetMakeRuntimeShape", "Scaled shapes cannot be parametrized.");
226 return nullptr;
227}
228
229////////////////////////////////////////////////////////////////////////////////
230/// Returns numbers of vertices, segments and polygons composing the shape mesh.
231
232void TGeoScaledShape::GetMeshNumbers(Int_t &nvert, Int_t &nsegs, Int_t &npols) const
233{
234 fShape->GetMeshNumbers(nvert, nsegs, npols);
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// print shape parameters
239
241{
242 printf("*** Shape %s: TGeoScaledShape ***\n", GetName());
243 fScale->Print();
244 fShape->InspectShape();
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Returns true if the scaled shape is an assembly.
250
252{
253 return fShape->IsAssembly();
254}
255
256////////////////////////////////////////////////////////////////////////////////
257/// Check if the scale transformation is a reflection.
258
260{
261 return fScale->IsReflection();
262}
263
264////////////////////////////////////////////////////////////////////////////////
265/// Creates a TBuffer3D describing *this* shape.
266/// Coordinates are in local reference frame.
267
269{
270 TBuffer3D *buff = fShape->MakeBuffer3D();
271 if (buff)
272 SetPoints(buff->fPnts);
273 return buff;
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// Create a scaled shape starting from a non-scaled one.
278
280{
281 TGeoShape *old_shape = shape;
282 TGeoShape *new_shape = nullptr;
283 if (shape->IsA() == TGeoScaledShape::Class()) {
284 TGeoScaledShape *sshape = (TGeoScaledShape *)shape;
285 TGeoScale *old_scale = sshape->GetScale();
286 old_shape = sshape->GetShape();
287 scale->SetScale(scale->GetScale()[0] * old_scale->GetScale()[0], scale->GetScale()[1] * old_scale->GetScale()[1],
288 scale->GetScale()[2] * old_scale->GetScale()[2]);
289 }
290 if (old_shape->IsAssembly()) {
291 // The shape is owned by the assembly, so make sure it does not register to TGeoManager
292 new_shape = new TGeoScaledShape(old_shape, scale);
293 new_shape->SetName(name);
294 } else {
295 new_shape = new TGeoScaledShape(name, old_shape, scale);
296 }
297 return new_shape;
298}
299
300////////////////////////////////////////////////////////////////////////////////
301/// Fill TBuffer3D structure for segments and polygons.
302
304{
305 fShape->SetSegsAndPols(buff);
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// computes the closest distance from given point to this shape, according
310/// to option. The matching point on the shape is stored in spoint.
311
313{
314 Double_t local[3];
315 fScale->MasterToLocal(point, local);
316 Double_t safe = fShape->Safety(local, in);
317 safe = fScale->LocalToMaster(safe);
318 return safe;
319}
320
321////////////////////////////////////////////////////////////////////////////////
322/// Save a primitive as a C++ statement(s) on output stream "out".
323
324void TGeoScaledShape::SavePrimitive(std::ostream &out, Option_t *option)
325{
327 return;
328 out << " // Shape: " << GetName() << " type: " << ClassName() << std::endl;
329 if (!fShape || !fScale) {
330 out << "##### Invalid shape or scale !. Aborting. #####" << std::endl;
331 return;
332 }
333 fShape->SavePrimitive(out, option);
334 TString sname = fShape->GetPointerName();
335 const Double_t *sc = fScale->GetScale();
336 out << " // Scale factor:" << std::endl;
337 out << " auto pScale_" << GetPointerName() << " = new TGeoScale(\"" << fScale->GetName() << "\"," << sc[0] << ","
338 << sc[1] << "," << sc[2] << ");" << std::endl;
339 out << " TGeoScaledShape *" << GetPointerName() << " = new TGeoScaledShape(\"" << GetName() << "\"," << sname
340 << ", pScale_" << GetPointerName() << " );" << std::endl;
341}
342
343////////////////////////////////////////////////////////////////////////////////
344/// Mesh points for scaled shapes.
345
347{
348 Int_t npts = fShape->GetNmeshVertices();
349 fShape->SetPoints(points);
350 Double_t master[3];
351 for (Int_t i = 0; i < npts; i++) {
352 fScale->LocalToMaster(&points[3 * i], master);
353 memcpy(&points[3 * i], master, 3 * sizeof(Double_t));
354 }
355}
356
357////////////////////////////////////////////////////////////////////////////////
358/// Mesh points for scaled shapes.
359
361{
362 Int_t npts = fShape->GetNmeshVertices();
363 fShape->SetPoints(points);
364 Double_t master[3];
365 Double_t local[3];
366 Int_t index;
367 for (Int_t i = 0; i < npts; i++) {
368 index = 3 * i;
369 local[0] = points[index];
370 local[1] = points[index + 1];
371 local[2] = points[index + 2];
372 fScale->LocalToMaster(local, master);
373 points[index] = master[0];
374 points[index + 1] = master[1];
375 points[index + 2] = master[2];
376 }
377}
378
379////////////////////////////////////////////////////////////////////////////////
380/// Check the inside status for each of the points in the array.
381/// Input: Array of point coordinates + vector size
382/// Output: Array of Booleans for the inside of each point
383
384void TGeoScaledShape::Contains_v(const Double_t *points, Bool_t *inside, Int_t vecsize) const
385{
386 for (Int_t i = 0; i < vecsize; i++)
387 inside[i] = Contains(&points[3 * i]);
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Compute the normal for an array o points so that norm.dot.dir is positive
392/// Input: Arrays of point coordinates and directions + vector size
393/// Output: Array of normal directions
394
395void TGeoScaledShape::ComputeNormal_v(const Double_t *points, const Double_t *dirs, Double_t *norms, Int_t vecsize)
396{
397 for (Int_t i = 0; i < vecsize; i++)
398 ComputeNormal(&points[3 * i], &dirs[3 * i], &norms[3 * i]);
399}
400
401////////////////////////////////////////////////////////////////////////////////
402/// Compute distance from array of input points having directions specified by dirs. Store output in dists
403
404void TGeoScaledShape::DistFromInside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize,
405 Double_t *step) const
406{
407 for (Int_t i = 0; i < vecsize; i++)
408 dists[i] = DistFromInside(&points[3 * i], &dirs[3 * i], 3, step[i]);
409}
410
411////////////////////////////////////////////////////////////////////////////////
412/// Compute distance from array of input points having directions specified by dirs. Store output in dists
413
415 Double_t *step) const
416{
417 for (Int_t i = 0; i < vecsize; i++)
418 dists[i] = DistFromOutside(&points[3 * i], &dirs[3 * i], 3, step[i]);
419}
420
421////////////////////////////////////////////////////////////////////////////////
422/// Compute safe distance from each of the points in the input array.
423/// Input: Array of point coordinates, array of statuses for these points, size of the arrays
424/// Output: Safety values
425
426void TGeoScaledShape::Safety_v(const Double_t *points, const Bool_t *inside, Double_t *safe, Int_t vecsize) const
427{
428 for (Int_t i = 0; i < vecsize; i++)
429 safe[i] = Safety(&points[3 * i], inside[i]);
430}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
char name[80]
Definition TGX11.cxx:148
point * points
Definition X3DBuffer.c:22
Generic 3D primitive description class.
Definition TBuffer3D.h:18
UInt_t NbPnts() const
Definition TBuffer3D.h:89
Bool_t SectionsValid(UInt_t mask) const
Definition TBuffer3D.h:76
Bool_t fLocalFrame
Definition TBuffer3D.h:99
Bool_t fScaled
Definition TBuffer3D.h:101
void SetAABoundingBox(const Double_t origin[3], const Double_t halfLengths[3])
Set fBBVertex in kBoundingBox section to a axis aligned (local) BB using supplied origin and box half...
Double_t * fPnts
Definition TBuffer3D.h:122
Double_t fBBVertex[8][3]
Definition TBuffer3D.h:117
Double_t fDX
Definition TGeoBBox.h:21
TGeoBBox(const TGeoBBox &)=delete
Double_t fOrigin[3]
Definition TGeoBBox.h:24
void InspectShape() const override
Double_t fDY
Definition TGeoBBox.h:22
Double_t fDZ
Definition TGeoBBox.h:23
Geometrical transformation package.
Definition TGeoMatrix.h:39
static void Normalize(Double_t *vect)
Normalize a vector.
Class describing scale transformations.
Definition TGeoMatrix.h:254
const Double_t * GetScale() const override
Definition TGeoMatrix.h:306
void SetScale(Double_t sx, Double_t sy, Double_t sz)
scale setter
Bool_t Contains(const Double_t *point) const override
Test if point is inside the scaled shape.
TGeoShape * GetMakeRuntimeShape(TGeoShape *mother, TGeoMatrix *mat) const override
in case shape has some negative parameters, these has to be computed in order to fit the mother
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
compute closest distance from point px,py to each vertex. Should not be called.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save a primitive as a C++ statement(s) on output stream "out".
TGeoScaledShape()
Default constructor.
TGeoShape * GetShape() const
void DistFromOutside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t *step) const override
Compute distance from array of input points having directions specified by dirs. Store output in dist...
Bool_t IsReflected() const override
Check if the scale transformation is a reflection.
~TGeoScaledShape() override
destructor
void ComputeNormal(const Double_t *point, const Double_t *dir, Double_t *norm) const override
Compute normal to closest surface from POINT.
TGeoShape * fShape
TGeoScale * fScale
TGeoVolume * Divide(TGeoVolume *voldiv, const char *divname, Int_t iaxis, Int_t ndiv, Double_t start, Double_t step) override
Cannot divide assemblies.
void InspectShape() const override
print shape parameters
void Safety_v(const Double_t *points, const Bool_t *inside, Double_t *safe, Int_t vecsize) const override
Compute safe distance from each of the points in the input array.
static TClass * Class()
const TBuffer3D & GetBuffer3D(Int_t reqSections, Bool_t localFrame) const override
Fills a static 3D buffer and returns a reference.
Double_t Safety(const Double_t *point, Bool_t in=kTRUE) const override
computes the closest distance from given point to this shape, according to option.
Bool_t IsAssembly() const override
Returns true if the scaled shape is an assembly.
void SetSegsAndPols(TBuffer3D &buffer) const override
Fill TBuffer3D structure for segments and polygons.
Double_t DistFromOutside(const Double_t *point, const Double_t *dir, Int_t iact=1, Double_t step=TGeoShape::Big(), Double_t *safe=nullptr) const override
Compute distance from outside point to surface of the scaled shape.
void SetPoints(Double_t *points) const override
Mesh points for scaled shapes.
Double_t Capacity() const override
Computes capacity of this shape [length^3].
static TGeoShape * MakeScaledShape(const char *name, TGeoShape *shape, TGeoScale *scale)
Create a scaled shape starting from a non-scaled one.
void Contains_v(const Double_t *points, Bool_t *inside, Int_t vecsize) const override
Check the inside status for each of the points in the array.
void GetMeshNumbers(Int_t &nvert, Int_t &nsegs, Int_t &npols) const override
Returns numbers of vertices, segments and polygons composing the shape mesh.
Double_t DistFromInside(const Double_t *point, const Double_t *dir, Int_t iact=1, Double_t step=TGeoShape::Big(), Double_t *safe=nullptr) const override
Compute distance from inside point to surface of the scaled shape.
TBuffer3D * MakeBuffer3D() const override
Creates a TBuffer3D describing this shape.
TGeoScale * GetScale() const
void DistFromInside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t *step) const override
Compute distance from array of input points having directions specified by dirs. Store output in dist...
void ComputeNormal_v(const Double_t *points, const Double_t *dirs, Double_t *norms, Int_t vecsize) override
Compute the normal for an array o points so that norm.dot.dir is positive Input: Arrays of point coor...
void ComputeBBox() override
Compute bounding box of the scaled shape.
void TransformPoints(Double_t *points, UInt_t NbPoints) const
Tranform a set of points (LocalToMaster).
void SetShapeBit(UInt_t f, Bool_t set)
Equivalent of TObject::SetBit.
TGeoShape()
Default constructor.
const char * GetPointerName() const
Provide a pointer name containing uid.
Int_t ShapeDistancetoPrimitive(Int_t numpoints, Int_t px, Int_t py) const
Returns distance to shape primitive mesh.
const char * GetName() const override
Get the shape name.
@ kGeoSavePrimitive
Definition TGeoShape.h:65
TClass * IsA() const override
Definition TGeoShape.h:181
virtual Bool_t IsAssembly() const
Definition TGeoShape.h:138
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:227
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
Basic string class.
Definition TString.h:138
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
const Int_t n
Definition legend1.C:16
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122