| [831] | 1 | //
|
|---|
| 2 | // ********************************************************************
|
|---|
| 3 | // * License and Disclaimer *
|
|---|
| 4 | // * *
|
|---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of *
|
|---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and *
|
|---|
| 7 | // * conditions of the Geant4 Software License, included in the file *
|
|---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These *
|
|---|
| 9 | // * include a list of copyright holders. *
|
|---|
| 10 | // * *
|
|---|
| 11 | // * Neither the authors of this software system, nor their employing *
|
|---|
| 12 | // * institutes,nor the agencies providing financial support for this *
|
|---|
| 13 | // * work make any representation or warranty, express or implied, *
|
|---|
| 14 | // * regarding this software system or assume any liability for its *
|
|---|
| 15 | // * use. Please see the license in the file LICENSE and URL above *
|
|---|
| 16 | // * for the full disclaimer and the limitation of liability. *
|
|---|
| 17 | // * *
|
|---|
| 18 | // * This code implementation is the result of the scientific and *
|
|---|
| 19 | // * technical work of the GEANT4 collaboration. *
|
|---|
| 20 | // * By using, copying, modifying or distributing the software (or *
|
|---|
| 21 | // * any work based on the software) you agree to acknowledge its *
|
|---|
| 22 | // * use in resulting scientific publications, and indicate your *
|
|---|
| 23 | // * acceptance of all terms of the Geant4 Software license. *
|
|---|
| 24 | // ********************************************************************
|
|---|
| 25 | //
|
|---|
| 26 | //
|
|---|
| 27 | // $Id: G4ExtrudedSolid.cc,v 1.11.2.1 2008/04/23 08:10:24 gcosmo Exp $
|
|---|
| 28 | // GEANT4 tag $Name: geant4-09-01-patch-02 $
|
|---|
| 29 | //
|
|---|
| 30 | //
|
|---|
| 31 | // --------------------------------------------------------------------
|
|---|
| 32 | // GEANT 4 class source file
|
|---|
| 33 | //
|
|---|
| 34 | // G4ExtrudedSolid.cc
|
|---|
| 35 | //
|
|---|
| 36 | // Author: Ivana Hrivnacova, IPN Orsay
|
|---|
| 37 | // --------------------------------------------------------------------
|
|---|
| 38 |
|
|---|
| 39 | #include <set>
|
|---|
| 40 | #include <algorithm>
|
|---|
| 41 | #include <cmath>
|
|---|
| 42 |
|
|---|
| 43 | #include "G4ExtrudedSolid.hh"
|
|---|
| 44 | #include "G4TriangularFacet.hh"
|
|---|
| 45 | #include "G4QuadrangularFacet.hh"
|
|---|
| 46 |
|
|---|
| 47 | //_____________________________________________________________________________
|
|---|
| 48 |
|
|---|
| 49 | G4ExtrudedSolid::G4ExtrudedSolid( const G4String& pName,
|
|---|
| 50 | std::vector<G4TwoVector> polygon,
|
|---|
| 51 | std::vector<ZSection> zsections)
|
|---|
| 52 | : G4TessellatedSolid(pName),
|
|---|
| 53 | fNv(polygon.size()),
|
|---|
| 54 | fNz(zsections.size()),
|
|---|
| 55 | fPolygon(),
|
|---|
| 56 | fZSections(),
|
|---|
| 57 | fTriangles(),
|
|---|
| 58 | fIsConvex(false),
|
|---|
| 59 | fGeometryType("G4ExtrudedSolid")
|
|---|
| 60 |
|
|---|
| 61 | {
|
|---|
| 62 | // General constructor
|
|---|
| 63 |
|
|---|
| 64 | // First check input parameters
|
|---|
| 65 |
|
|---|
| 66 | if ( fNv < 3 ) {
|
|---|
| 67 | G4Exception(
|
|---|
| 68 | "G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 69 | FatalException, "Number of polygon vertices < 3");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if ( fNz < 2 ) {
|
|---|
| 73 | G4Exception(
|
|---|
| 74 | "G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 75 | FatalException, "Number of z-sides < 2");
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | for ( G4int i=0; i<fNz-1; ++i )
|
|---|
| 79 | {
|
|---|
| 80 | if ( zsections[i].fZ > zsections[i+1].fZ )
|
|---|
| 81 | {
|
|---|
| 82 | G4Exception(
|
|---|
| 83 | "G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 84 | FatalException,
|
|---|
| 85 | "Z-sections have to be ordered by z value (z0 < z1 < z2 ...)");
|
|---|
| 86 | }
|
|---|
| 87 | if ( std::fabs( zsections[i+1].fZ - zsections[i].fZ ) < kCarTolerance )
|
|---|
| 88 | {
|
|---|
| 89 | G4Exception(
|
|---|
| 90 | "G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 91 | FatalException,
|
|---|
| 92 | "Z-sections with the same z position are not supported.");
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | // Copy polygon
|
|---|
| 97 | //
|
|---|
| 98 | for ( G4int i=0; i<fNv; ++i ) { fPolygon.push_back(polygon[i]); }
|
|---|
| 99 |
|
|---|
| 100 | // Copy z-sections
|
|---|
| 101 | //
|
|---|
| 102 | for ( G4int i=0; i<fNz; ++i ) { fZSections.push_back(zsections[i]); }
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | G4bool result = MakeFacets();
|
|---|
| 106 | if (!result)
|
|---|
| 107 | {
|
|---|
| 108 | G4Exception("G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 109 | FatalException, "Making facets failed.");
|
|---|
| 110 | }
|
|---|
| 111 | fIsConvex = IsConvex();
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | ComputeProjectionParameters();
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | //_____________________________________________________________________________
|
|---|
| 118 |
|
|---|
| 119 | G4ExtrudedSolid::G4ExtrudedSolid( const G4String& pName,
|
|---|
| 120 | std::vector<G4TwoVector> polygon,
|
|---|
| 121 | G4double dz,
|
|---|
| 122 | G4TwoVector off1, G4double scale1,
|
|---|
| 123 | G4TwoVector off2, G4double scale2 )
|
|---|
| 124 | : G4TessellatedSolid(pName),
|
|---|
| 125 | fNv(polygon.size()),
|
|---|
| 126 | fNz(2),
|
|---|
| 127 | fPolygon(),
|
|---|
| 128 | fZSections(),
|
|---|
| 129 | fTriangles(),
|
|---|
| 130 | fIsConvex(false),
|
|---|
| 131 | fGeometryType("G4ExtrudedSolid")
|
|---|
| 132 |
|
|---|
| 133 | {
|
|---|
| 134 | // Special constructor for solid with 2 z-sections
|
|---|
| 135 |
|
|---|
| 136 | // First check input parameters
|
|---|
| 137 | //
|
|---|
| 138 | if ( fNv < 3 )
|
|---|
| 139 | {
|
|---|
| 140 | G4Exception("G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 141 | FatalException, "Number of polygon vertices < 3");
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | // Copy polygon
|
|---|
| 145 | //
|
|---|
| 146 | for ( G4int i=0; i<fNv; ++i ) { fPolygon.push_back(polygon[i]); }
|
|---|
| 147 |
|
|---|
| 148 | // Copy z-sections
|
|---|
| 149 | //
|
|---|
| 150 | fZSections.push_back(ZSection(-dz, off1, scale1));
|
|---|
| 151 | fZSections.push_back(ZSection( dz, off2, scale2));
|
|---|
| 152 |
|
|---|
| 153 | G4bool result = MakeFacets();
|
|---|
| 154 | if (!result)
|
|---|
| 155 | {
|
|---|
| 156 | G4Exception("G4ExtrudedSolid::G4ExtrudedSolid()", "InvalidSetup",
|
|---|
| 157 | FatalException, "Making facets failed.");
|
|---|
| 158 | }
|
|---|
| 159 | fIsConvex = IsConvex();
|
|---|
| 160 |
|
|---|
| 161 | ComputeProjectionParameters();
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | //_____________________________________________________________________________
|
|---|
| 165 |
|
|---|
| 166 | G4ExtrudedSolid::G4ExtrudedSolid( __void__& a )
|
|---|
| 167 | : G4TessellatedSolid(a)
|
|---|
| 168 | {
|
|---|
| 169 | // Fake default constructor - sets only member data and allocates memory
|
|---|
| 170 | // for usage restricted to object persistency.
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 | //_____________________________________________________________________________
|
|---|
| 175 |
|
|---|
| 176 | G4ExtrudedSolid::~G4ExtrudedSolid()
|
|---|
| 177 | {
|
|---|
| 178 | // Destructor
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | //_____________________________________________________________________________
|
|---|
| 182 |
|
|---|
| 183 | void G4ExtrudedSolid::ComputeProjectionParameters()
|
|---|
| 184 | {
|
|---|
| 185 | // Compute parameters for point projections p(z)
|
|---|
| 186 | // to the polygon scale & offset:
|
|---|
| 187 | // scale(z) = k*z + scale0
|
|---|
| 188 | // offset(z) = l*z + offset0
|
|---|
| 189 | // p(z) = scale(z)*p0 + offset(z)
|
|---|
| 190 | // p0 = (p(z) - offset(z))/scale(z);
|
|---|
| 191 | //
|
|---|
| 192 |
|
|---|
| 193 | for ( G4int iz=0; iz<fNz-1; ++iz)
|
|---|
| 194 | {
|
|---|
| 195 | G4double z1 = fZSections[iz].fZ;
|
|---|
| 196 | G4double z2 = fZSections[iz+1].fZ;
|
|---|
| 197 | G4double scale1 = fZSections[iz].fScale;
|
|---|
| 198 | G4double scale2 = fZSections[iz+1].fScale;
|
|---|
| 199 | G4TwoVector off1 = fZSections[iz].fOffset;
|
|---|
| 200 | G4TwoVector off2 = fZSections[iz+1].fOffset;
|
|---|
| 201 |
|
|---|
| 202 | G4double kscale = (scale2 - scale1)/(z2 - z1);
|
|---|
| 203 | G4double scale0 = scale2 - kscale*(z2 - z1)/2.0;
|
|---|
| 204 | G4TwoVector koff = (off2 - off1)/(z2 - z1);
|
|---|
| 205 | G4TwoVector off0 = off2 - koff*(z2 - z1)/2.0;
|
|---|
| 206 |
|
|---|
| 207 | fKScales.push_back(kscale);
|
|---|
| 208 | fScale0s.push_back(scale0);
|
|---|
| 209 | fKOffsets.push_back(koff);
|
|---|
| 210 | fOffset0s.push_back(off0);
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | //_____________________________________________________________________________
|
|---|
| 216 |
|
|---|
| 217 | G4ThreeVector G4ExtrudedSolid::GetVertex(G4int iz, G4int ind) const
|
|---|
| 218 | {
|
|---|
| 219 | // Shift and scale vertices
|
|---|
| 220 |
|
|---|
| 221 | return G4ThreeVector( fPolygon[ind].x() * fZSections[iz].fScale
|
|---|
| 222 | + fZSections[iz].fOffset.x(),
|
|---|
| 223 | fPolygon[ind].y() * fZSections[iz].fScale
|
|---|
| 224 | + fZSections[iz].fOffset.y(), fZSections[iz].fZ);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | //_____________________________________________________________________________
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 | G4TwoVector G4ExtrudedSolid::ProjectPoint(const G4ThreeVector& point) const
|
|---|
| 231 | {
|
|---|
| 232 | // Project point in the polygon scale
|
|---|
| 233 | // scale(z) = k*z + scale0
|
|---|
| 234 | // offset(z) = l*z + offset0
|
|---|
| 235 | // p(z) = scale(z)*p0 + offset(z)
|
|---|
| 236 | // p0 = (p(z) - offset(z))/scale(z);
|
|---|
| 237 |
|
|---|
| 238 | // Select projection (z-segment of the solid) according to p.z()
|
|---|
| 239 | //
|
|---|
| 240 | G4int iz = 0;
|
|---|
| 241 | while ( point.z() > fZSections[iz+1].fZ && iz < fNz-2 ) { ++iz; }
|
|---|
| 242 |
|
|---|
| 243 | G4double z0 = ( fZSections[iz+1].fZ + fZSections[iz].fZ )/2.0;
|
|---|
| 244 | G4TwoVector p2(point.x(), point.y());
|
|---|
| 245 | G4double pscale = fKScales[iz]*(point.z()-z0) + fScale0s[iz];
|
|---|
| 246 | G4TwoVector poffset = fKOffsets[iz]*(point.z()-z0) + fOffset0s[iz];
|
|---|
| 247 |
|
|---|
| 248 | // G4cout << point << " projected to "
|
|---|
| 249 | // << iz << "-th z-segment polygon as "
|
|---|
| 250 | // << (p2 - poffset)/pscale << G4endl;
|
|---|
| 251 |
|
|---|
| 252 | // pscale is always >0 as it is an interpolation between two
|
|---|
| 253 | // positive scale values
|
|---|
| 254 | //
|
|---|
| 255 | return (p2 - poffset)/pscale;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | //_____________________________________________________________________________
|
|---|
| 259 |
|
|---|
| 260 | G4bool G4ExtrudedSolid::IsSameLine(G4TwoVector p,
|
|---|
| 261 | G4TwoVector l1, G4TwoVector l2) const
|
|---|
| 262 | {
|
|---|
| 263 | // Return true if p is on the line through l1, l2
|
|---|
| 264 |
|
|---|
| 265 | if ( l1.x() == l2.x() )
|
|---|
| 266 | {
|
|---|
| 267 | return std::fabs(p.x() - l1.x()) < kCarTolerance;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | return std::fabs (p.y() - l1.y() - ((l2.y() - l1.y())/(l2.x() - l1.x()))
|
|---|
| 271 | *(p.x() - l1.x())) < kCarTolerance;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | //_____________________________________________________________________________
|
|---|
| 275 |
|
|---|
| 276 | G4bool G4ExtrudedSolid::IsSameLineSegment(G4TwoVector p,
|
|---|
| 277 | G4TwoVector l1, G4TwoVector l2) const
|
|---|
| 278 | {
|
|---|
| 279 | // Return true if p is on the line through l1, l2 and lies between
|
|---|
| 280 | // l1 and l2
|
|---|
| 281 |
|
|---|
| 282 | if ( p.x() < std::min(l1.x(), l2.x()) - kCarTolerance ||
|
|---|
| 283 | p.x() > std::max(l1.x(), l2.x()) + kCarTolerance ||
|
|---|
| 284 | p.y() < std::min(l1.y(), l2.y()) - kCarTolerance||
|
|---|
| 285 | p.y() > std::max(l1.y(), l2.y()) + kCarTolerance )
|
|---|
| 286 | {
|
|---|
| 287 | return false;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | return IsSameLine(p, l1, l2);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | //_____________________________________________________________________________
|
|---|
| 294 |
|
|---|
| 295 | G4bool G4ExtrudedSolid::IsSameSide(G4TwoVector p1, G4TwoVector p2,
|
|---|
| 296 | G4TwoVector l1, G4TwoVector l2) const
|
|---|
| 297 | {
|
|---|
| 298 | // Return true if p1 and p2 are on the same side of the line through l1, l2
|
|---|
| 299 |
|
|---|
| 300 | return ( (p1.x() - l1.x()) * (l2.y() - l1.y())
|
|---|
| 301 | - (l2.x() - l1.x()) * (p1.y() - l1.y()) )
|
|---|
| 302 | * ( (p2.x() - l1.x()) * (l2.y() - l1.y())
|
|---|
| 303 | - (l2.x() - l1.x()) * (p2.y() - l1.y()) ) > 0;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | //_____________________________________________________________________________
|
|---|
| 307 |
|
|---|
| 308 | G4bool G4ExtrudedSolid::IsPointInside(G4TwoVector a, G4TwoVector b,
|
|---|
| 309 | G4TwoVector c, G4TwoVector p) const
|
|---|
| 310 | {
|
|---|
| 311 | // Return true if p is inside of triangle abc, else returns false
|
|---|
| 312 |
|
|---|
| 313 | // Check extent first
|
|---|
| 314 | //
|
|---|
| 315 | if ( ( p.x() < a.x() && p.x() < b.x() && p.x() < c.x() ) ||
|
|---|
| 316 | ( p.x() > a.x() && p.x() > b.x() && p.x() > c.x() ) ||
|
|---|
| 317 | ( p.y() < a.y() && p.y() < b.y() && p.y() < c.y() ) ||
|
|---|
| 318 | ( p.y() > a.y() && p.y() > b.y() && p.y() > c.y() ) ) return false;
|
|---|
| 319 |
|
|---|
| 320 | return IsSameSide(p, a, b, c)
|
|---|
| 321 | && IsSameSide(p, b, a, c)
|
|---|
| 322 | && IsSameSide(p, c, a, b);
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | //_____________________________________________________________________________
|
|---|
| 326 |
|
|---|
| 327 | G4double
|
|---|
| 328 | G4ExtrudedSolid::GetAngle(G4TwoVector po, G4TwoVector pa, G4TwoVector pb) const
|
|---|
| 329 | {
|
|---|
| 330 | // Return the angle of the vertex in po
|
|---|
| 331 |
|
|---|
| 332 | G4TwoVector t1 = pa - po;
|
|---|
| 333 | G4TwoVector t2 = pb - po;
|
|---|
| 334 |
|
|---|
| 335 | G4double result
|
|---|
| 336 | = (atan2(t1.y(), t1.x()) - atan2(t2.y(), t2.x()));
|
|---|
| 337 |
|
|---|
| 338 | if ( result < 0 ) result += 2*pi;
|
|---|
| 339 |
|
|---|
| 340 | return result;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | //_____________________________________________________________________________
|
|---|
| 344 |
|
|---|
| 345 | G4VFacet*
|
|---|
| 346 | G4ExtrudedSolid::MakeDownFacet(G4int ind1, G4int ind2, G4int ind3) const
|
|---|
| 347 | {
|
|---|
| 348 | // Create a triangular facet from the polygon points given by indices
|
|---|
| 349 | // forming the down side ( the normal goes in -z)
|
|---|
| 350 |
|
|---|
| 351 | std::vector<G4ThreeVector> vertices;
|
|---|
| 352 | vertices.push_back(GetVertex(0, ind1));
|
|---|
| 353 | vertices.push_back(GetVertex(0, ind2));
|
|---|
| 354 | vertices.push_back(GetVertex(0, ind3));
|
|---|
| 355 |
|
|---|
| 356 | // first vertex most left
|
|---|
| 357 | //
|
|---|
| 358 | G4ThreeVector cross
|
|---|
| 359 | = (vertices[1]-vertices[0]).cross(vertices[2]-vertices[1]);
|
|---|
| 360 |
|
|---|
| 361 | if ( cross.z() > 0.0 )
|
|---|
| 362 | {
|
|---|
| 363 | // vertices ardered clock wise has to be reordered
|
|---|
| 364 |
|
|---|
| 365 | // G4cout << "G4ExtrudedSolid::MakeDownFacet: reordering vertices "
|
|---|
| 366 | // << ind1 << ", " << ind2 << ", " << ind3 << G4endl;
|
|---|
| 367 |
|
|---|
| 368 | G4ThreeVector tmp = vertices[1];
|
|---|
| 369 | vertices[1] = vertices[2];
|
|---|
| 370 | vertices[2] = tmp;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | return new G4TriangularFacet(vertices[0], vertices[1],
|
|---|
| 374 | vertices[2], ABSOLUTE);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | //_____________________________________________________________________________
|
|---|
| 378 |
|
|---|
| 379 | G4VFacet*
|
|---|
| 380 | G4ExtrudedSolid::MakeUpFacet(G4int ind1, G4int ind2, G4int ind3) const
|
|---|
| 381 | {
|
|---|
| 382 | // Creates a triangular facet from the polygon points given by indices
|
|---|
| 383 | // forming the upper side ( z>0 )
|
|---|
| 384 |
|
|---|
| 385 | std::vector<G4ThreeVector> vertices;
|
|---|
| 386 | vertices.push_back(GetVertex(fNz-1, ind1));
|
|---|
| 387 | vertices.push_back(GetVertex(fNz-1, ind2));
|
|---|
| 388 | vertices.push_back(GetVertex(fNz-1, ind3));
|
|---|
| 389 |
|
|---|
| 390 | // first vertex most left
|
|---|
| 391 | //
|
|---|
| 392 | G4ThreeVector cross
|
|---|
| 393 | = (vertices[1]-vertices[0]).cross(vertices[2]-vertices[1]);
|
|---|
| 394 |
|
|---|
| 395 | if ( cross.z() < 0.0 )
|
|---|
| 396 | {
|
|---|
| 397 | // vertices ordered clock wise has to be reordered
|
|---|
| 398 |
|
|---|
| 399 | // G4cout << "G4ExtrudedSolid::MakeUpFacet: reordering vertices "
|
|---|
| 400 | // << ind1 << ", " << ind2 << ", " << ind3 << G4endl;
|
|---|
| 401 |
|
|---|
| 402 | G4ThreeVector tmp = vertices[1];
|
|---|
| 403 | vertices[1] = vertices[2];
|
|---|
| 404 | vertices[2] = tmp;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | return new G4TriangularFacet(vertices[0], vertices[1],
|
|---|
| 408 | vertices[2], ABSOLUTE);
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | //_____________________________________________________________________________
|
|---|
| 412 |
|
|---|
| 413 | G4bool G4ExtrudedSolid::AddGeneralPolygonFacets()
|
|---|
| 414 | {
|
|---|
| 415 | // Decompose polygonal sides in triangular facets
|
|---|
| 416 |
|
|---|
| 417 | typedef std::pair < G4TwoVector, G4int > Vertex;
|
|---|
| 418 |
|
|---|
| 419 | // Fill one more vector
|
|---|
| 420 | //
|
|---|
| 421 | std::vector< Vertex > verticesToBeDone;
|
|---|
| 422 | for ( G4int i=0; i<fNv; ++i )
|
|---|
| 423 | {
|
|---|
| 424 | verticesToBeDone.push_back(Vertex(fPolygon[i], i));
|
|---|
| 425 | }
|
|---|
| 426 | std::vector< Vertex > ears;
|
|---|
| 427 |
|
|---|
| 428 | std::vector< Vertex >::iterator c1 = verticesToBeDone.begin();
|
|---|
| 429 | std::vector< Vertex >::iterator c2 = c1+1;
|
|---|
| 430 | std::vector< Vertex >::iterator c3 = c1+2;
|
|---|
| 431 | while ( verticesToBeDone.size()>2 )
|
|---|
| 432 | {
|
|---|
| 433 |
|
|---|
| 434 | // G4cout << "Looking at triangle : "
|
|---|
| 435 | // << c1->second << " " << c2->second
|
|---|
| 436 | // << " " << c3->second << G4endl;
|
|---|
| 437 |
|
|---|
| 438 | // skip concave vertices
|
|---|
| 439 | //
|
|---|
| 440 | G4double angle = GetAngle(c2->first, c3->first, c1->first);
|
|---|
| 441 | //G4cout << angle << G4endl;
|
|---|
| 442 | if ( angle > pi ) {
|
|---|
| 443 | // G4cout << "Skipping concave vertex " << c2->second << G4endl;
|
|---|
| 444 |
|
|---|
| 445 | // try next three consecutive vertices
|
|---|
| 446 | //
|
|---|
| 447 | c1 = c2;
|
|---|
| 448 | c2 = c3;
|
|---|
| 449 | ++c3;
|
|---|
| 450 | if ( c3 == verticesToBeDone.end() ) { c3 = verticesToBeDone.begin(); }
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | G4bool good = true;
|
|---|
| 454 | std::vector< Vertex >::iterator it;
|
|---|
| 455 | for ( it=verticesToBeDone.begin(); it != verticesToBeDone.end(); ++it )
|
|---|
| 456 | {
|
|---|
| 457 | // skip vertices of tested triangle
|
|---|
| 458 | //
|
|---|
| 459 | if ( it == c1 || it == c2 || it == c3 ) { continue; }
|
|---|
| 460 |
|
|---|
| 461 | if ( IsPointInside(c1->first, c2->first, c3->first, it->first) )
|
|---|
| 462 | {
|
|---|
| 463 | // G4cout << "Point " << it->second << " is inside" << G4endl;
|
|---|
| 464 | good = false;
|
|---|
| 465 |
|
|---|
| 466 | // try next three consecutive vertices
|
|---|
| 467 | //
|
|---|
| 468 | c1 = c2;
|
|---|
| 469 | c2 = c3;
|
|---|
| 470 | ++c3;
|
|---|
| 471 | if ( c3 == verticesToBeDone.end() ) { c3 = verticesToBeDone.begin(); }
|
|---|
| 472 | break;
|
|---|
| 473 | }
|
|---|
| 474 | // else
|
|---|
| 475 | // { G4cout << "Point " << it->second << " is outside" << G4endl; }
|
|---|
| 476 | }
|
|---|
| 477 | if ( good )
|
|---|
| 478 | {
|
|---|
| 479 | // all points are outside triangle, we can make a facet
|
|---|
| 480 |
|
|---|
| 481 | // G4cout << "Found triangle : "
|
|---|
| 482 | // << c1->second << " " << c2->second
|
|---|
| 483 | // << " " << c3->second << G4endl;
|
|---|
| 484 |
|
|---|
| 485 | G4bool result;
|
|---|
| 486 | result = AddFacet( MakeDownFacet(c1->second, c2->second, c3->second) );
|
|---|
| 487 | if ( ! result ) { return false; }
|
|---|
| 488 |
|
|---|
| 489 | result = AddFacet( MakeUpFacet(c1->second, c2->second, c3->second) );
|
|---|
| 490 | if ( ! result ) { return false; }
|
|---|
| 491 |
|
|---|
| 492 | std::vector<G4int> triangle(3);
|
|---|
| 493 | triangle[0] = c1->second;
|
|---|
| 494 | triangle[1] = c2->second;
|
|---|
| 495 | triangle[2] = c3->second;
|
|---|
| 496 | fTriangles.push_back(triangle);
|
|---|
| 497 |
|
|---|
| 498 | // remove the ear point from verticesToBeDone
|
|---|
| 499 | //
|
|---|
| 500 | verticesToBeDone.erase(c2);
|
|---|
| 501 | c1 = verticesToBeDone.begin();
|
|---|
| 502 | c2 = c1+1;
|
|---|
| 503 | c3 = c1+2;
|
|---|
| 504 | }
|
|---|
| 505 | }
|
|---|
| 506 | return true;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | //_____________________________________________________________________________
|
|---|
| 510 |
|
|---|
| 511 | G4bool G4ExtrudedSolid::MakeFacets()
|
|---|
| 512 | {
|
|---|
| 513 | // Define facets
|
|---|
| 514 |
|
|---|
| 515 | G4bool good;
|
|---|
| 516 |
|
|---|
| 517 | // Decomposition of polygonal sides in the facets
|
|---|
| 518 | //
|
|---|
| 519 | if ( fNv == 3 )
|
|---|
| 520 | {
|
|---|
| 521 | good = AddFacet( new G4TriangularFacet( GetVertex(0, 0), GetVertex(0, 1),
|
|---|
| 522 | GetVertex(0, 2), ABSOLUTE) );
|
|---|
| 523 | if ( ! good ) { return false; }
|
|---|
| 524 |
|
|---|
| 525 | good = AddFacet( new G4TriangularFacet( GetVertex(fNz-1, 2), GetVertex(fNz-1, 1),
|
|---|
| 526 | GetVertex(fNz-1, 0), ABSOLUTE) );
|
|---|
| 527 | if ( ! good ) { return false; }
|
|---|
| 528 |
|
|---|
| 529 | std::vector<G4int> triangle(3);
|
|---|
| 530 | triangle[0] = 0;
|
|---|
| 531 | triangle[1] = 1;
|
|---|
| 532 | triangle[2] = 2;
|
|---|
| 533 | fTriangles.push_back(triangle);
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | else if ( fNv == 4 )
|
|---|
| 537 | {
|
|---|
| 538 | good = AddFacet( new G4QuadrangularFacet( GetVertex(0, 0),GetVertex(0, 1),
|
|---|
| 539 | GetVertex(0, 2),GetVertex(0, 3),
|
|---|
| 540 | ABSOLUTE) );
|
|---|
| 541 | if ( ! good ) { return false; }
|
|---|
| 542 |
|
|---|
| 543 | good = AddFacet( new G4QuadrangularFacet( GetVertex(fNz-1, 3), GetVertex(fNz-1, 2),
|
|---|
| 544 | GetVertex(fNz-1, 1), GetVertex(1, 0),
|
|---|
| 545 | ABSOLUTE) );
|
|---|
| 546 | if ( ! good ) { return false; }
|
|---|
| 547 |
|
|---|
| 548 | std::vector<G4int> triangle1(3);
|
|---|
| 549 | triangle1[0] = 0;
|
|---|
| 550 | triangle1[1] = 1;
|
|---|
| 551 | triangle1[2] = 2;
|
|---|
| 552 | fTriangles.push_back(triangle1);
|
|---|
| 553 |
|
|---|
| 554 | std::vector<G4int> triangle2(3);
|
|---|
| 555 | triangle2[0] = 0;
|
|---|
| 556 | triangle2[1] = 2;
|
|---|
| 557 | triangle2[2] = 3;
|
|---|
| 558 | fTriangles.push_back(triangle2);
|
|---|
| 559 | }
|
|---|
| 560 | else
|
|---|
| 561 | {
|
|---|
| 562 | good = AddGeneralPolygonFacets();
|
|---|
| 563 | if ( ! good ) { return false; }
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | // The quadrangular sides
|
|---|
| 567 | //
|
|---|
| 568 | for ( G4int iz = 0; iz < fNz-1; ++iz )
|
|---|
| 569 | {
|
|---|
| 570 | for ( G4int i = 0; i < fNv; ++i )
|
|---|
| 571 | {
|
|---|
| 572 | G4int j = (i+1) % fNv;
|
|---|
| 573 | good = AddFacet( new G4QuadrangularFacet
|
|---|
| 574 | ( GetVertex(iz, j), GetVertex(iz, i),
|
|---|
| 575 | GetVertex(iz+1, i), GetVertex(iz+1, j), ABSOLUTE) );
|
|---|
| 576 | if ( ! good ) { return false; }
|
|---|
| 577 | }
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | SetSolidClosed(true);
|
|---|
| 581 |
|
|---|
| 582 | return good;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | //_____________________________________________________________________________
|
|---|
| 586 |
|
|---|
| 587 | G4bool G4ExtrudedSolid::IsConvex() const
|
|---|
| 588 | {
|
|---|
| 589 | // Get polygon convexity (polygon is convex if all vertex angles are < pi )
|
|---|
| 590 |
|
|---|
| 591 | for ( G4int i=0; i< fNv; ++i )
|
|---|
| 592 | {
|
|---|
| 593 | G4int j = ( i + 1 ) % fNv;
|
|---|
| 594 | G4int k = ( i + 2 ) % fNv;
|
|---|
| 595 | G4TwoVector v1 = fPolygon[i]-fPolygon[j];
|
|---|
| 596 | G4TwoVector v2 = fPolygon[k]-fPolygon[j];
|
|---|
| 597 | G4double dphi = v2.phi() - v1.phi();
|
|---|
| 598 | if ( dphi < 0. ) { dphi += 2.*pi; }
|
|---|
| 599 |
|
|---|
| 600 | if ( dphi >= pi ) { return false; }
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | return true;
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | //_____________________________________________________________________________
|
|---|
| 607 |
|
|---|
| 608 | G4GeometryType G4ExtrudedSolid::GetEntityType () const
|
|---|
| 609 | {
|
|---|
| 610 | // Return entity type
|
|---|
| 611 |
|
|---|
| 612 | return fGeometryType;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | //_____________________________________________________________________________
|
|---|
| 616 |
|
|---|
| 617 | EInside G4ExtrudedSolid::Inside (const G4ThreeVector &p) const
|
|---|
| 618 | {
|
|---|
| 619 | // Override the base class function as it fails in case of concave polygon.
|
|---|
| 620 | // Project the point in the original polygon scale and check if it is inside
|
|---|
| 621 | // for each triangle.
|
|---|
| 622 |
|
|---|
| 623 | // Check first if outside extent
|
|---|
| 624 | //
|
|---|
| 625 | if ( p.x() < GetMinXExtent() - kCarTolerance ||
|
|---|
| 626 | p.x() > GetMaxXExtent() + kCarTolerance ||
|
|---|
| 627 | p.y() < GetMinYExtent() - kCarTolerance ||
|
|---|
| 628 | p.y() > GetMaxYExtent() + kCarTolerance ||
|
|---|
| 629 | p.z() < GetMinZExtent() - kCarTolerance ||
|
|---|
| 630 | p.z() > GetMaxZExtent() + kCarTolerance )
|
|---|
| 631 | {
|
|---|
| 632 | // G4cout << "G4ExtrudedSolid::Outside extent: " << p << G4endl;
|
|---|
| 633 | return kOutside;
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| 636 | // Project point p(z) to the polygon scale p0
|
|---|
| 637 | //
|
|---|
| 638 | G4TwoVector pscaled = ProjectPoint(p);
|
|---|
| 639 |
|
|---|
| 640 | // Check if on surface of polygon
|
|---|
| 641 | //
|
|---|
| 642 | for ( G4int i=0; i<fNv; ++i )
|
|---|
| 643 | {
|
|---|
| 644 | G4int j = (i+1) % fNv;
|
|---|
| 645 | if ( IsSameLine(pscaled, fPolygon[i], fPolygon[j]) )
|
|---|
| 646 | {
|
|---|
| 647 | // G4cout << "G4ExtrudedSolid::Inside return Surface (on polygon) "
|
|---|
| 648 | // << G4endl;
|
|---|
| 649 |
|
|---|
| 650 | return kSurface;
|
|---|
| 651 | }
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | // Now check if inside triangles
|
|---|
| 655 | //
|
|---|
| 656 | std::vector< std::vector<G4int> >::const_iterator it = fTriangles.begin();
|
|---|
| 657 | G4bool inside = false;
|
|---|
| 658 | do
|
|---|
| 659 | {
|
|---|
| 660 | if ( IsPointInside(fPolygon[(*it)[0]], fPolygon[(*it)[1]],
|
|---|
| 661 | fPolygon[(*it)[2]], pscaled) ) { inside = true; }
|
|---|
| 662 |
|
|---|
| 663 | if ( IsSameLineSegment(pscaled, fPolygon[(*it)[0]], fPolygon[(*it)[1]]) ||
|
|---|
| 664 | IsSameLineSegment(pscaled, fPolygon[(*it)[1]], fPolygon[(*it)[2]]) ||
|
|---|
| 665 | IsSameLineSegment(pscaled, fPolygon[(*it)[2]], fPolygon[(*it)[0]]) )
|
|---|
| 666 | { inside = true; }
|
|---|
| 667 | ++it;
|
|---|
| 668 | } while ( (inside == false) && (it != fTriangles.end()) );
|
|---|
| 669 |
|
|---|
| 670 | if ( inside )
|
|---|
| 671 | {
|
|---|
| 672 | // Check if on surface of z sides
|
|---|
| 673 | //
|
|---|
| 674 | if ( std::fabs( p.z() - fZSections[0].fZ ) < kCarTolerance ||
|
|---|
| 675 | std::fabs( p.z() - fZSections[fNz-1].fZ ) < kCarTolerance )
|
|---|
| 676 | {
|
|---|
| 677 | // G4cout << "G4ExtrudedSolid::Inside return Surface (on z side)"
|
|---|
| 678 | // << G4endl;
|
|---|
| 679 |
|
|---|
| 680 | return kSurface;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | // G4cout << "G4ExtrudedSolid::Inside return Inside" << G4endl;
|
|---|
| 684 |
|
|---|
| 685 | return kInside;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | // G4cout << "G4ExtrudedSolid::Inside return Outside " << G4endl;
|
|---|
| 689 |
|
|---|
| 690 | return kOutside;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | //_____________________________________________________________________________
|
|---|
| 694 |
|
|---|
| 695 | G4double G4ExtrudedSolid::DistanceToOut (const G4ThreeVector &p,
|
|---|
| 696 | const G4ThreeVector &v,
|
|---|
| 697 | const G4bool calcNorm,
|
|---|
| 698 | G4bool *validNorm,
|
|---|
| 699 | G4ThreeVector *n) const
|
|---|
| 700 | {
|
|---|
| 701 | // Override the base class function to redefine validNorm
|
|---|
| 702 | // (the solid can be concave)
|
|---|
| 703 |
|
|---|
| 704 | G4double distOut =
|
|---|
| 705 | G4TessellatedSolid::DistanceToOut(p, v, calcNorm, validNorm, n);
|
|---|
| 706 | if (validNorm) { *validNorm = fIsConvex; }
|
|---|
| 707 |
|
|---|
| 708 | return distOut;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 | //_____________________________________________________________________________
|
|---|
| 713 |
|
|---|
| 714 | G4double G4ExtrudedSolid::DistanceToOut (const G4ThreeVector &p) const
|
|---|
| 715 | {
|
|---|
| 716 | // Override the overloaded base class function
|
|---|
| 717 |
|
|---|
| 718 | return G4TessellatedSolid::DistanceToOut(p);
|
|---|
| 719 | }
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 | //_____________________________________________________________________________
|
|---|
| 723 |
|
|---|
| 724 | std::ostream& G4ExtrudedSolid::StreamInfo(std::ostream &os) const
|
|---|
| 725 | {
|
|---|
| 726 | os << "-----------------------------------------------------------\n"
|
|---|
| 727 | << " *** Dump for solid - " << GetName() << " ***\n"
|
|---|
| 728 | << " ===================================================\n"
|
|---|
| 729 | << " Solid geometry type: " << fGeometryType << G4endl;
|
|---|
| 730 |
|
|---|
| 731 | if ( fIsConvex)
|
|---|
| 732 | { os << " Convex polygon; list of vertices:" << G4endl; }
|
|---|
| 733 | else
|
|---|
| 734 | { os << " Concave polygon; list of vertices:" << G4endl; }
|
|---|
| 735 |
|
|---|
| 736 | for ( G4int i=0; i<fNv; ++i )
|
|---|
| 737 | {
|
|---|
| 738 | os << " vx = " << fPolygon[i].x()/mm << " mm"
|
|---|
| 739 | << " vy = " << fPolygon[i].y()/mm << " mm" << G4endl;
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | os << " Sections:" << G4endl;
|
|---|
| 743 | for ( G4int iz=0; iz<fNz; ++iz )
|
|---|
| 744 | {
|
|---|
| 745 | os << " z = " << fZSections[iz].fZ/mm << " mm "
|
|---|
| 746 | << " x0= " << fZSections[iz].fOffset.x()/mm << " mm "
|
|---|
| 747 | << " y0= " << fZSections[iz].fOffset.y()/mm << " mm "
|
|---|
| 748 | << " scale= " << fZSections[iz].fScale << G4endl;
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | return os;
|
|---|
| 752 | }
|
|---|