#include "Riostream.h"
#include "TGeoManager.h"
#include "TGeoMatrix.h"
#include "TGeoVolume.h"
#include "TGeoNode.h"
#include "TGeoScaledShape.h"
#include "TBuffer3D.h"
#include "TBuffer3DTypes.h"
#include "TMath.h"
/*
<img src="gif/TGeoScaledShape.gif">
*/
//End_Html
ClassImp(TGeoScaledShape)
TGeoScaledShape::TGeoScaledShape()
{
fShape = 0;
fScale = 0;
}
TGeoScaledShape::TGeoScaledShape(const char *name, TGeoShape *shape, TGeoScale *scale)
:TGeoBBox(name,0,0,0)
{
fShape = shape;
fScale = scale;
if (!fScale->IsRegistered()) fScale->RegisterYourself();
ComputeBBox();
}
TGeoScaledShape::TGeoScaledShape(TGeoShape *shape, TGeoScale *scale)
{
fShape = shape;
fScale = scale;
if (!fScale->IsRegistered()) fScale->RegisterYourself();
ComputeBBox();
}
TGeoScaledShape::~TGeoScaledShape()
{
}
Double_t TGeoScaledShape::Capacity() const
{
Double_t capacity = fShape->Capacity();
const Double_t *scale = fScale->GetScale();
capacity *= scale[0]*scale[1]*scale[2];
return capacity;
}
void TGeoScaledShape::ComputeBBox()
{
if (!fShape) {
Error("ComputeBBox", "Scaled shape %s without shape", GetName());
return;
}
if (fShape->IsAssembly()) fShape->ComputeBBox();
TGeoBBox *box = (TGeoBBox*)fShape;
const Double_t *orig = box->GetOrigin();
Double_t point[3], master[3];
point[0] = box->GetDX();
point[1] = box->GetDY();
point[2] = box->GetDZ();
fScale->LocalToMaster(orig, fOrigin);
fScale->LocalToMaster(point, master);
fDX = TMath::Abs(master[0]);
fDY = TMath::Abs(master[1]);
fDZ = TMath::Abs(master[2]);
}
void TGeoScaledShape::ComputeNormal(const Double_t *point, const Double_t *dir, Double_t *norm)
{
Double_t local[3], ldir[3], lnorm[3];
fScale->MasterToLocal(point,local);
fScale->MasterToLocalVect(dir,ldir);
TGeoMatrix::Normalize(ldir);
fShape->ComputeNormal(local,ldir,lnorm);
fScale->MasterToLocalVect(lnorm, norm);
TGeoMatrix::Normalize(norm);
}
Bool_t TGeoScaledShape::Contains(const Double_t *point) const
{
Double_t local[3];
fScale->MasterToLocal(point,local);
return fShape->Contains(local);
}
Int_t TGeoScaledShape::DistancetoPrimitive(Int_t px, Int_t py)
{
Int_t n = fShape->GetNmeshVertices();
return ShapeDistancetoPrimitive(n, px, py);
}
Double_t TGeoScaledShape::DistFromInside(const Double_t *point, const Double_t *dir, Int_t iact, Double_t step, Double_t *safe) const
{
Double_t local[3], ldir[3];
Double_t lstep;
fScale->MasterToLocal(point,local);
lstep = fScale->MasterToLocal(step, dir);
fScale->MasterToLocalVect(dir,ldir);
TGeoMatrix::Normalize(ldir);
Double_t dist = fShape->DistFromInside(local,ldir, iact, lstep, safe);
if (iact<3 && safe) *safe = fScale->LocalToMaster(*safe);
dist = fScale->LocalToMaster(dist, ldir);
return dist;
}
Double_t TGeoScaledShape::DistFromOutside(const Double_t *point, const Double_t *dir, Int_t iact, Double_t step, Double_t *safe) const
{
Double_t local[3], ldir[3];
Double_t lstep;
fScale->MasterToLocal(point,local);
lstep = fScale->MasterToLocal(step, dir);
fScale->MasterToLocalVect(dir,ldir);
TGeoMatrix::Normalize(ldir);
Double_t dist = fShape->DistFromOutside(local,ldir, iact, lstep, safe);
if (safe) *safe = fScale->LocalToMaster(*safe);
dist = fScale->LocalToMaster(dist, ldir);
return dist;
}
TGeoVolume *TGeoScaledShape::Divide(TGeoVolume * , const char *divname, Int_t , Int_t ,
Double_t , Double_t )
{
Error("Divide", "Scaled shapes cannot be divided. Division volume %s not created", divname);
return 0;
}
const TBuffer3D & TGeoScaledShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
{
TBuffer3D &buffer = (TBuffer3D &)fShape->GetBuffer3D(reqSections, localFrame);
Double_t halfLengths[3] = { fDX, fDY, fDZ };
buffer.SetAABoundingBox(fOrigin, halfLengths);
if (!buffer.fLocalFrame) {
TransformPoints(buffer.fBBVertex[0], 8);
}
if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
SetPoints(buffer.fPnts);
if (!buffer.fLocalFrame) {
TransformPoints(buffer.fPnts, buffer.NbPnts());
}
}
return buffer;
}
TGeoShape *TGeoScaledShape::GetMakeRuntimeShape(TGeoShape * , TGeoMatrix * ) const
{
Error("GetMakeRuntimeShape", "Scaled shapes cannot be parametrized.");
return NULL;
}
void TGeoScaledShape::GetMeshNumbers(Int_t &nvert, Int_t &nsegs, Int_t &npols) const
{
fShape->GetMeshNumbers(nvert, nsegs, npols);
}
void TGeoScaledShape::InspectShape() const
{
printf("*** Shape %s: TGeoScaledShape ***\n", GetName());
fScale->Print();
fShape->InspectShape();
TGeoBBox::InspectShape();
}
Bool_t TGeoScaledShape::IsAssembly() const
{
return fShape->IsAssembly();
}
Bool_t TGeoScaledShape::IsReflected() const
{
return fScale->IsReflection();
}
TBuffer3D *TGeoScaledShape::MakeBuffer3D() const
{
TBuffer3D *buff = fShape->MakeBuffer3D();
if (buff) SetPoints(buff->fPnts);
return buff;
}
TGeoShape *TGeoScaledShape::MakeScaledShape(const char *name, TGeoShape *shape, TGeoScale *scale)
{
TGeoShape *new_shape;
if (shape->IsA() == TGeoScaledShape::Class()) {
TGeoScaledShape *sshape = (TGeoScaledShape*)shape;
TGeoScale *old_scale = sshape->GetScale();
TGeoShape *old_shape = sshape->GetShape();
scale->SetScale(scale->GetScale()[0]*old_scale->GetScale()[0],
scale->GetScale()[1]*old_scale->GetScale()[1],
scale->GetScale()[2]*old_scale->GetScale()[2]);
new_shape = new TGeoScaledShape(name, old_shape, scale);
return new_shape;
}
new_shape = new TGeoScaledShape(name, shape, scale);
return new_shape;
}
void TGeoScaledShape::SetSegsAndPols(TBuffer3D &buff) const
{
fShape->SetSegsAndPols(buff);
}
Double_t TGeoScaledShape::Safety(const Double_t *point, Bool_t in) const
{
Double_t local[3];
fScale->MasterToLocal(point,local);
Double_t safe = fShape->Safety(local,in);
safe = fScale->LocalToMaster(safe);
return safe;
}
void TGeoScaledShape::SavePrimitive(std::ostream &out, Option_t *option)
{
if (TObject::TestBit(kGeoSavePrimitive)) return;
out << " // Shape: " << GetName() << " type: " << ClassName() << std::endl;
if (!fShape || !fScale) {
out << "##### Invalid shape or scale !. Aborting. #####" << std::endl;
return;
}
fShape->SavePrimitive(out, option);
TString sname = fShape->GetPointerName();
const Double_t *sc = fScale->GetScale();
out << " // Scale factor:" << std::endl;
out << " TGeoScale *pScale = new TGeoScale(\"" << fScale->GetName()
<< "\"," << sc[0] << "," << sc[1] << "," << sc[2] << ");" << std::endl;
out << " TGeoScaledShape *" << GetPointerName() << " = new TGeoScaledShape(\""
<< GetName() << "\"," << sname << ", pScale);" << std::endl;
}
void TGeoScaledShape::SetPoints(Double_t *points) const
{
Int_t npts = fShape->GetNmeshVertices();
fShape->SetPoints(points);
Double_t master[3];
for (Int_t i=0; i<npts; i++) {
fScale->LocalToMaster(&points[3*i], master);
memcpy(&points[3*i], master, 3*sizeof(Double_t));
}
}
void TGeoScaledShape::SetPoints(Float_t *points) const
{
Int_t npts = fShape->GetNmeshVertices();
fShape->SetPoints(points);
Double_t master[3];
Double_t local[3];
Int_t index;
for (Int_t i=0; i<npts; i++) {
index = 3*i;
local[0] = points[index];
local[1] = points[index+1];
local[2] = points[index+2];
fScale->LocalToMaster(local, master);
points[index] = master[0];
points[index+1] = master[1];
points[index+2] = master[2];
}
}
void TGeoScaledShape::Contains_v(const Double_t *points, Bool_t *inside, Int_t vecsize) const
{
for (Int_t i=0; i<vecsize; i++) inside[i] = Contains(&points[3*i]);
}
void TGeoScaledShape::ComputeNormal_v(const Double_t *points, const Double_t *dirs, Double_t *norms, Int_t vecsize)
{
for (Int_t i=0; i<vecsize; i++) ComputeNormal(&points[3*i], &dirs[3*i], &norms[3*i]);
}
void TGeoScaledShape::DistFromInside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step) const
{
for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromInside(&points[3*i], &dirs[3*i], 3, step[i]);
}
void TGeoScaledShape::DistFromOutside_v(const Double_t *points, const Double_t *dirs, Double_t *dists, Int_t vecsize, Double_t* step) const
{
for (Int_t i=0; i<vecsize; i++) dists[i] = DistFromOutside(&points[3*i], &dirs[3*i], 3, step[i]);
}
void TGeoScaledShape::Safety_v(const Double_t *points, const Bool_t *inside, Double_t *safe, Int_t vecsize) const
{
for (Int_t i=0; i<vecsize; i++) safe[i] = Safety(&points[3*i], inside[i]);
}