ATLAS Offline Software
ObjHelper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // This file was largely imported from the Acts testing framework
6 
7 #include "ObjHelper.h"
8 
9 #include <vector>
10 
11 void
12 ObjHelper::writeVTN(std::ofstream& stream,
13  VtnCounter& vtnCounter,
14  double scalor,
15  const Acts::Vector3& vertex,
16  const std::string& vtntype,
17  bool point)
18 {
19  // in case you make a point
20  unsigned int cp = 0;
21  // the counter
22  if (vtntype == "v") {
23  ++vtnCounter.vcounter;
24  cp = vtnCounter.vcounter;
25  } else if (vtntype == "t") {
26  ++vtnCounter.vtcounter;
27  cp = vtnCounter.vtcounter;
28  } else if (vtntype == "n") {
29  ++vtnCounter.ncounter;
30  cp = vtnCounter.ncounter;
31  } else
32  return;
33 
34  // write out the vertex, texture vertex, normal
35  stream << vtntype << " " << scalor * vertex.x() << " " << scalor * vertex.y()
36  << " " << scalor * vertex.z() << '\n';
37  // we create a point if needed
38  if (point) stream << "p " << cp;
39 }
40 
41 void
43  std::ofstream& stream,
44  unsigned int start,
45  const std::vector<unsigned int>& vsides)
46 {
47  // construct the vertical faces
48  size_t nsides = vsides.size();
49  unsigned int sstart = start;
50  for (auto vside : vsides) {
51  if (vside) {
52  // start streaming the side
53  // all but the last
54  if (start - sstart < nsides - 1) {
55  stream << "f " << start << " " << start + 1 << " ";
56  stream << start + nsides + 1 << " " << start + nsides;
57  } else {
58  stream << "f " << start << " " << sstart << " ";
59  stream << sstart + nsides << " " << start + nsides;
60  }
61  }
62  stream << '\n';
63  // increase
64  ++start;
65  }
66 }
67 
68 void
70  VtnCounter& vtnCounter,
71  double scalor,
72  const std::vector<Acts::Vector3>& vertices,
73  double thickness,
74  const std::vector<unsigned int>& vsides)
75 {
76  // minimum 3 vertices needed
77  if (vertices.size() < 3) return;
78  // the first vertex
79  unsigned int fvertex = vtnCounter.vcounter + 1;
80  // lets create the normal vector first
81  Acts::Vector3 sideOne = vertices[1] - vertices[0];
82  Acts::Vector3 sideTwo = vertices[2] - vertices[1];
83  Acts::Vector3 nvector(sideTwo.cross(sideOne).normalized());
84  // write the normal vector
85  writeVTN(stream, vtnCounter, scalor, nvector, "n");
86  // thickness or not thickness
87  std::vector<int> sides = {1};
88  if (thickness != 0.) sides = {-1, 1};
89  // now write all the vertices - this works w/wo thickness
90  for (auto side : sides) {
91  // save the current vertex counter
92  unsigned int cvc = vtnCounter.vcounter;
93  // loop over the sides
94  for (auto v : vertices)
96  vtnCounter,
97  scalor,
98  v + (0.5 * side * thickness) * nvector,
99  "v");
100  // decide if you want to add texture
101  std::string vtphr
102  = "/"; // vtnCounter.vtcounter ? "/"+std::to_string(vtcounter) : "/";
103  std::string ntphr = "/" + std::to_string(vtnCounter.ncounter);
104  // now write the face
105  stream << "f ";
106  for (size_t i=0;i<vertices.size();++i) stream << ++cvc << vtphr << ntphr << " ";
107  stream << '\n';
108  }
109  // now process the vertical sides
110  constructVerticalFaces(stream, fvertex, vsides);
111 }
112 
113 void
115  VtnCounter& vtnCounter,
116  double scalor,
117  unsigned int nSegments,
118  const Acts::Transform3& transform,
119  double r,
120  double hZ,
121  double thickness)
122 {
123  // flip along plus/minus and declare the faces
124  std::vector<int> flip = {-1, 1};
125  std::vector<int> vfaces = {1, 2, 4, 3};
126  // the number of phisteps
127  double phistep = 2 * M_PI / nSegments;
128  // make it twice if necessary
129  std::vector<double> roffsets = {0.};
130  if (thickness != 0.) roffsets = {-0.5 * thickness, 0.5 * thickness};
131  // now loop over the thickness and make an outer and inner
132  unsigned int cvc = vtnCounter.vcounter;
133  size_t iside = 0;
134  for (auto t : roffsets) {
135  size_t iphi = 0;
136  // loop over phi steps
137  for (; iphi < nSegments; ++iphi) {
138  // currentPhi
139  double phi = -M_PI + iphi * phistep;
140  for (auto iflip : flip) {
141  // create the vertex
142  Acts::Vector3 point(transform * Acts::Vector3((r + t) * cos(phi),
143  (r + t) * sin(phi),
144  iflip * hZ));
145  // write the normal vector
146  writeVTN(stream, vtnCounter, scalor, point, "v");
147  }
148  }
149  // now create the faces
150  iphi = 0;
151  // side offset for faces
152  unsigned int soff = 2 * iside * nSegments;
153  for (; iphi < nSegments - 1; ++iphi) {
154  // output to file
155  stream << "f ";
156  for (auto face : vfaces) stream << soff + cvc + (2 * iphi) + face << " ";
157  stream << '\n';
158  }
159  // close the loop
160  stream << "f " << soff + cvc + (2 * iphi) + 1 << " "
161  << soff + cvc + (2 * iphi) + 2 << " " << soff + cvc + 2 << " "
162  << soff + cvc + 1 << '\n';
163  // new line at the end of the line
164  stream << '\n';
165  ++iside;
166  }
167 
168  // construct the sides at the end when all vertices are done
169  Acts::Vector3 nvectorSide = transform.rotation().col(2);
170  // write the normal vector @todo flip sides
171  writeVTN(stream, vtnCounter, scalor, nvectorSide, "n");
172  std::string ntphr = "//" + std::to_string(vtnCounter.ncounter);
173 
174  if (thickness != 0.) {
175  // loop over the two sides
176  for (iside = 0; iside < 2; ++iside) {
177  // rest iphi
178  size_t iphi = 0;
179  for (; iphi < nSegments - 1; ++iphi) {
180  stream << "f ";
181  unsigned int base = cvc + (2 * iphi) + 1;
182  stream << iside + base << ntphr << " ";
183  stream << iside + base + 2 << ntphr << " ";
184  stream << iside + base + (2 * nSegments) + 2 << ntphr << " ";
185  stream << iside + base + (2 * nSegments) << ntphr << '\n';
186  }
187  // close the loop
188  stream << "f ";
189  stream << iside + cvc + (2 * iphi) + 1 << ntphr << " ";
190  stream << iside + cvc + 1 << ntphr << " ";
191  stream << iside + cvc + 1 + (2 * nSegments) << ntphr << " ";
192  stream << iside + cvc + (2 * iphi) + 1 + (2 * nSegments) << ntphr << '\n';
193  }
194  }
195 }
base
std::string base
Definition: hcg.cxx:78
beamspotman.r
def r
Definition: beamspotman.py:676
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
ObjHelper::VtnCounter::vcounter
unsigned int vcounter
Definition: ObjHelper.h:19
RPDUtils::sides
constexpr std::initializer_list< unsigned int > sides
Definition: RPDUtils.h:17
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
M_PI
#define M_PI
Definition: ActiveFraction.h:11
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
TRT::Hit::side
@ side
Definition: HitInfo.h:83
lumiFormat.i
int i
Definition: lumiFormat.py:85
ObjHelper::writePlanarFace
void writePlanarFace(std::ofstream &stream, VtnCounter &vtnCounter, double scalor, const std::vector< Acts::Vector3 > &vertices, double thickness=0., const std::vector< unsigned int > &vsides={})
This will write a planar face.
Definition: ObjHelper.cxx:69
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
ObjHelper::writeTube
void writeTube(std::ofstream &stream, VtnCounter &vtnCounter, double scalor, unsigned int nSegments, const Acts::Transform3 &transform, double r, double hZ, double thickness=0.)
This will write a cylindrical object.
Definition: ObjHelper.cxx:114
ObjHelper::VtnCounter::vtcounter
unsigned int vtcounter
Definition: ObjHelper.h:20
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
LArNewCalib_PedestalAutoCorr.cp
cp
Definition: LArNewCalib_PedestalAutoCorr.py:185
ObjHelper::VtnCounter::ncounter
unsigned int ncounter
Definition: ObjHelper.h:21
python.PyAthena.v
v
Definition: PyAthena.py:154
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
ObjHelper::VtnCounter
This is the counter struct for keeping track of the vertices.
Definition: ObjHelper.h:18
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
ObjHelper.h
ObjHelper::constructVerticalFaces
void constructVerticalFaces(std::ofstream &stream, unsigned int start, const std::vector< unsigned int > &vsides)
construct vertical faces this takes a range and constructs faces
Definition: ObjHelper.cxx:42
ObjHelper::writeVTN
void writeVTN(std::ofstream &stream, VtnCounter &vtnCounter, double scalor, const Acts::Vector3 &vertex, const std::string &vtntype="v", bool point=false)
This will write a vertex to the fstream.
Definition: ObjHelper.cxx:12