00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "TGLPhysicalShape.h"
00013 #include "TGLLogicalShape.h"
00014 #include "TGLPShapeRef.h"
00015 #include "TGLCamera.h"
00016 #include "TGLRnrCtx.h"
00017 #include "TGLIncludes.h"
00018
00019 #include "TGLScene.h"
00020
00021 #include "TColor.h"
00022 #include "TROOT.h"
00023
00024 #include <cmath>
00025
00026
00027 #include "TClass.h"
00028 #include "TError.h"
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 ClassImp(TGLPhysicalShape)
00061
00062
00063 TGLPhysicalShape::TGLPhysicalShape(UInt_t id, const TGLLogicalShape & logicalShape,
00064 const TGLMatrix & transform, Bool_t invertedWind,
00065 const Float_t rgba[4]) :
00066 fLogicalShape (&logicalShape),
00067 fNextPhysical (0),
00068 fFirstPSRef (0),
00069 fID (id),
00070 fTransform (transform),
00071 fSelected (0),
00072 fInvertedWind (invertedWind),
00073 fModified (kFALSE),
00074 fManip (kManipAll)
00075 {
00076
00077
00078
00079
00080
00081
00082
00083 fLogicalShape->AddRef(this);
00084 UpdateBoundingBox();
00085
00086
00087 InitColor(rgba);
00088 }
00089
00090
00091 TGLPhysicalShape::TGLPhysicalShape(UInt_t id, const TGLLogicalShape & logicalShape,
00092 const Double_t * transform, Bool_t invertedWind,
00093 const Float_t rgba[4]) :
00094 fLogicalShape (&logicalShape),
00095 fNextPhysical (0),
00096 fFirstPSRef (0),
00097 fID (id),
00098 fTransform (transform),
00099 fSelected (0),
00100 fInvertedWind (invertedWind),
00101 fModified (kFALSE),
00102 fManip (kManipAll)
00103 {
00104
00105
00106
00107
00108
00109
00110
00111 fLogicalShape->AddRef(this);
00112
00113
00114
00115
00116 fTransform.Transpose3x3();
00117 UpdateBoundingBox();
00118
00119
00120 InitColor(rgba);
00121 }
00122
00123
00124 TGLPhysicalShape::~TGLPhysicalShape()
00125 {
00126
00127
00128
00129
00130 if (fLogicalShape) fLogicalShape->SubRef(this);
00131
00132
00133 while (fFirstPSRef) {
00134 fFirstPSRef->SetPShape(0);
00135 }
00136 }
00137
00138
00139 void TGLPhysicalShape::AddReference(TGLPShapeRef* ref)
00140 {
00141
00142
00143 assert(ref != 0);
00144
00145 ref->fNextPSRef = fFirstPSRef;
00146 fFirstPSRef = ref;
00147 }
00148
00149
00150 void TGLPhysicalShape::RemoveReference(TGLPShapeRef* ref)
00151 {
00152
00153
00154 assert(ref != 0);
00155
00156 Bool_t found = kFALSE;
00157 if (fFirstPSRef == ref) {
00158 fFirstPSRef = ref->fNextPSRef;
00159 found = kTRUE;
00160 } else {
00161 TGLPShapeRef *shp1 = fFirstPSRef, *shp2;
00162 while ((shp2 = shp1->fNextPSRef) != 0) {
00163 if (shp2 == ref) {
00164 shp1->fNextPSRef = shp2->fNextPSRef;
00165 found = kTRUE;
00166 break;
00167 }
00168 shp1 = shp2;
00169 }
00170 }
00171 if (found) {
00172 ref->fNextPSRef = 0;
00173 } else {
00174 Error("TGLPhysicalShape::RemoveReference", "Attempt to un-ref an unregistered shape-ref.");
00175 }
00176 }
00177
00178
00179 void TGLPhysicalShape::Modified()
00180 {
00181
00182
00183
00184 fModified = kTRUE;
00185 TGLPShapeRef * ref = fFirstPSRef;
00186 while (ref) {
00187 ref->PShapeModified();
00188 ref = ref->fNextPSRef;
00189 }
00190 }
00191
00192
00193 void TGLPhysicalShape::UpdateBoundingBox()
00194 {
00195
00196
00197 fBoundingBox.Set(fLogicalShape->BoundingBox());
00198 fBoundingBox.Transform(fTransform);
00199
00200 if (fLogicalShape->GetScene())
00201 fLogicalShape->GetScene()->InvalidateBoundingBox();
00202 }
00203
00204
00205 void TGLPhysicalShape::InitColor(const Float_t rgba[4])
00206 {
00207
00208
00209
00210 fColor[0] = rgba[0];
00211 fColor[1] = rgba[1];
00212 fColor[2] = rgba[2];
00213 fColor[3] = rgba[3];
00214
00215 fColor[4] = fColor[5] = fColor[6] = 0.0f;
00216 fColor[8] = fColor[9] = fColor[10] = 0.7f;
00217 fColor[12] = fColor[13] = fColor[14] = 0.0f;
00218 fColor[7] = fColor[11] = fColor[15] = 1.0f;
00219 fColor[16] = 60.0f;
00220 }
00221
00222
00223 void TGLPhysicalShape::SetColor(const Float_t color[17])
00224 {
00225
00226
00227
00228
00229
00230 for (UInt_t i = 0; i < 17; i++) {
00231 fColor[i] = color[i];
00232 }
00233
00234 Modified();
00235 }
00236
00237
00238 void TGLPhysicalShape::SetColorOnFamily(const Float_t color[17])
00239 {
00240
00241
00242
00243 TGLPhysicalShape* pshp = const_cast<TGLPhysicalShape*>(fLogicalShape->GetFirstPhysical());
00244 while (pshp)
00245 {
00246 pshp->SetColor(color);
00247 pshp = pshp->fNextPhysical;
00248 }
00249 }
00250
00251
00252 void TGLPhysicalShape::SetDiffuseColor(const Float_t rgba[4])
00253 {
00254
00255
00256 for (Int_t i=0; i<4; ++i)
00257 fColor[i] = rgba[i];
00258 Modified();
00259 }
00260
00261
00262 void TGLPhysicalShape::SetDiffuseColor(const UChar_t rgba[4])
00263 {
00264
00265
00266 for (Int_t i=0; i<4; ++i)
00267 fColor[i] = rgba[i]/255.0f;
00268 Modified();
00269 }
00270
00271
00272 void TGLPhysicalShape::SetDiffuseColor(Color_t ci, UChar_t transparency)
00273 {
00274
00275
00276
00277 if (ci < 0) ci = 1;
00278 TColor* c = gROOT->GetColor(ci);
00279 if (c) {
00280 fColor[0] = c->GetRed();
00281 fColor[1] = c->GetGreen();
00282 fColor[2] = c->GetBlue();
00283 fColor[3] = 1.0f - 0.01*transparency;
00284 }
00285 Modified();
00286 }
00287
00288
00289 void TGLPhysicalShape::SetupGLColors(TGLRnrCtx & rnrCtx, const Float_t* color) const
00290 {
00291
00292
00293
00294 if (color == 0) color = fColor;
00295
00296 switch (rnrCtx.DrawPass()) {
00297 case TGLRnrCtx::kPassWireFrame:
00298 {
00299
00300 glColor4fv(color);
00301 break;
00302 }
00303 case TGLRnrCtx::kPassFill:
00304 case TGLRnrCtx::kPassOutlineFill:
00305 {
00306
00307
00308
00309
00310
00311
00312 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
00313 glMaterialfv(GL_FRONT, GL_AMBIENT, color + 4);
00314 glMaterialfv(GL_FRONT, GL_SPECULAR, color + 8);
00315 glMaterialfv(GL_FRONT, GL_EMISSION, color + 12);
00316 glMaterialf(GL_FRONT, GL_SHININESS, color[16]);
00317
00318 glColor4fv(color);
00319 break;
00320 }
00321 case TGLRnrCtx::kPassOutlineLine:
00322 {
00323
00324
00325 TGLUtil::ColorAlpha(rnrCtx.ColorSet().Outline(), 0.5f*color[3]);
00326 break;
00327 }
00328 default:
00329 {
00330 assert(kFALSE);
00331 }
00332 }
00333 }
00334
00335 static const Float_t selColor[] =
00336 {
00337 1, 1, 1, 1,
00338 0, 0, 0, 1,
00339 0, 0, 0, 1,
00340 0.5, 0, 0, 1,
00341 0
00342 };
00343
00344
00345 void TGLPhysicalShape::Draw(TGLRnrCtx & rnrCtx) const
00346 {
00347
00348
00349
00350 if (gDebug > 4) {
00351 Info("TGLPhysicalShape::Draw", "this %ld (class %s) LOD %d",
00352 (Long_t)this, IsA()->GetName(), rnrCtx.ShapeLOD());
00353 }
00354
00355
00356
00357 if (rnrCtx.ShapeLOD() == TGLRnrCtx::kLODPixel)
00358 {
00359 if (!rnrCtx.IsDrawPassOutlineLine())
00360 {
00361 glColor4fv(fColor);
00362 glBegin(GL_POINTS);
00363 glVertex3dv(&fTransform.CArr()[12]);
00364 glEnd();
00365 }
00366 return;
00367 }
00368
00369 if (gDebug > 4) {
00370 Info("TGLPhysicalShape::Draw", "this %ld (class %s) LOD %d",
00371 (Long_t)this, IsA()->GetName(), rnrCtx.ShapeLOD());
00372 }
00373
00374 glPushMatrix();
00375 glMultMatrixd(fTransform.CArr());
00376 if (fInvertedWind) glFrontFace(GL_CW);
00377 if (rnrCtx.Highlight() && !rnrCtx.Selection() && !rnrCtx.IsDrawPassOutlineLine())
00378 {
00379 fLogicalShape->DrawHighlight(rnrCtx, this);
00380 }
00381 else
00382 {
00383 SetupGLColors(rnrCtx);
00384 if (rnrCtx.IsDrawPassOutlineLine())
00385 TGLUtil::LockColor();
00386 fLogicalShape->Draw(rnrCtx);
00387 if (rnrCtx.IsDrawPassOutlineLine())
00388 TGLUtil::UnlockColor();
00389 }
00390 if (fInvertedWind) glFrontFace(GL_CCW);
00391 glPopMatrix();
00392 }
00393
00394
00395 void TGLPhysicalShape::CalculateShapeLOD(TGLRnrCtx& rnrCtx, Float_t& pixSize, Short_t& shapeLOD) const
00396 {
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 TGLLogicalShape::ELODAxes lodAxes = fLogicalShape->SupportedLODAxes();
00410
00411 if (lodAxes == TGLLogicalShape::kLODAxesNone)
00412 {
00413
00414
00415
00416 pixSize = 100;
00417 shapeLOD = TGLRnrCtx::kLODHigh;
00418 return;
00419 }
00420
00421 std::vector <Double_t> boxViewportDiags;
00422 const TGLBoundingBox & box = BoundingBox();
00423 const TGLCamera & camera = rnrCtx.RefCamera();
00424
00425 if (lodAxes == TGLLogicalShape::kLODAxesAll) {
00426
00427
00428 boxViewportDiags.push_back(camera.ViewportRect(box).Diagonal());
00429 } else if (lodAxes == (TGLLogicalShape::kLODAxesY | TGLLogicalShape::kLODAxesZ)) {
00430
00431
00432 boxViewportDiags.push_back(camera.ViewportRect(box, TGLBoundingBox::kFaceLowX).Diagonal());
00433 boxViewportDiags.push_back(camera.ViewportRect(box, TGLBoundingBox::kFaceHighX).Diagonal());
00434 } else if (lodAxes == (TGLLogicalShape::kLODAxesX | TGLLogicalShape::kLODAxesZ)) {
00435
00436 boxViewportDiags.push_back(camera.ViewportRect(box, TGLBoundingBox::kFaceLowY).Diagonal());
00437 boxViewportDiags.push_back(camera.ViewportRect(box, TGLBoundingBox::kFaceHighY).Diagonal());
00438 } else if (lodAxes == (TGLLogicalShape::kLODAxesX | TGLLogicalShape::kLODAxesY)) {
00439
00440 boxViewportDiags.push_back(camera.ViewportRect(box, TGLBoundingBox::kFaceLowZ).Diagonal());
00441 boxViewportDiags.push_back(camera.ViewportRect(box, TGLBoundingBox::kFaceHighZ).Diagonal());
00442 } else {
00443
00444
00445
00446
00447 Error("TGLPhysicalShape::CalcPhysicalLOD", "LOD calculation for single axis not implemented presently");
00448 shapeLOD = TGLRnrCtx::kLODMed;
00449 return;
00450 }
00451
00452
00453 Double_t largestDiagonal = 0.0;
00454 for (UInt_t i = 0; i < boxViewportDiags.size(); i++) {
00455 if (boxViewportDiags[i] > largestDiagonal) {
00456 largestDiagonal = boxViewportDiags[i];
00457 }
00458 }
00459 pixSize = largestDiagonal;
00460
00461 if (largestDiagonal <= 1.0) {
00462 shapeLOD = TGLRnrCtx::kLODPixel;
00463 } else {
00464
00465
00466
00467 UInt_t lodApp = static_cast<UInt_t>(std::pow(largestDiagonal,0.4) * 100.0 / std::pow(2000.0,0.4));
00468 if (lodApp > 1000) lodApp = 1000;
00469 shapeLOD = (Short_t) lodApp;
00470 }
00471 }
00472
00473
00474 void TGLPhysicalShape::QuantizeShapeLOD(Short_t shapeLOD, Short_t combiLOD, Short_t& quantLOD) const
00475 {
00476
00477
00478
00479 quantLOD = fLogicalShape->QuantizeShapeLOD(shapeLOD, combiLOD);
00480 }
00481
00482
00483 void TGLPhysicalShape::InvokeContextMenu(TContextMenu & menu, UInt_t x, UInt_t y) const
00484 {
00485
00486
00487
00488
00489 fLogicalShape->InvokeContextMenu(menu, x, y);
00490 }