3 #include "TriangleSet.h"
4 #include "RGBAPalette.h"
8 #include <TVirtualPad.h>
9 #include <TVirtualViewer3D.h>
10 #include <TBuffer3D.h>
11 #include <TBuffer3DTypes.h>
15 //______________________________________________________________________
18 // Made from a list of vertices and a list of triangles (triplets of
21 // If input is composed from triangles with direct vertex coordinates
22 // one should consider finding all occurences of the same vertex
23 // and specifying it only once.
27 TriangleSet::TriangleSet(Int_t nv, Int_t nt, Bool_t norms, Bool_t cols) :
28 RenderElement(fColor),
29 TNamed("TriangleSet", 0),
30 fNVerts (nv), fVerts(0),
31 fNTrings (nt), fTrings(0), fTringNorms(0), fTringCols(0),
35 fVerts = new Float_t[3*fNVerts];
36 fTrings = new Int_t [3*fNTrings];
37 fTringNorms = (norms) ? new Float_t[3*fNTrings] : 0;
38 fTringCols = (cols) ? new UChar_t[3*fNTrings] : 0;
41 TriangleSet::~TriangleSet()
45 delete [] fTringNorms;
49 /**************************************************************************/
51 void TriangleSet::GenerateTriangleNormals()
53 if (fTringNorms == 0) fTringNorms = new Float_t[3*fNTrings];
56 Float_t *N = fTringNorms;
58 for(Int_t t=0; t<fNTrings; ++t, N+=3, T+=3)
60 Float_t* v0 = Vertex(T[0]);
61 Float_t* v1 = Vertex(T[1]);
62 Float_t* v2 = Vertex(T[2]);
63 e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]);
64 e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]);
71 void TriangleSet::GenerateRandomColors()
73 if (fTringCols == 0) fTringCols = new UChar_t[3*fNTrings];
77 UChar_t *C = fTringCols;
78 for(Int_t t=0; t<fNTrings; ++t, C+=3)
80 C[0] = (UChar_t) r.Uniform(60, 255);
81 C[1] = (UChar_t) r.Uniform(60, 255);
82 C[2] = (UChar_t) r.Uniform(60, 255);
86 void TriangleSet::GenerateZNormalColors(Float_t fac, Int_t min, Int_t max,
87 Bool_t interp, Bool_t wrap)
89 if (fTringCols == 0) fTringCols = new UChar_t[3*fNTrings];
90 if (fTringNorms == 0) GenerateTriangleNormals();
92 RGBAPalette pal(min, max, interp, wrap);
93 UChar_t *C = fTringCols;
94 Float_t *N = fTringNorms;
95 for(Int_t t=0; t<fNTrings; ++t, C+=3, N+=3)
97 Int_t v = TMath::Nint(fac * N[2]);
98 pal.ColorFromValue(v, C, kFALSE);
100 gPad->Modified(); gPad->Update();
103 /**************************************************************************/
105 void TriangleSet::ComputeBBox()
114 for (Int_t i=0; i<fNVerts; ++i, v += 3)
118 void TriangleSet::Paint(Option_t* )
120 TBuffer3D buffer(TBuffer3DTypes::kGeneric);
124 buffer.fColor = fColor;
125 buffer.fTransparency = 0;
126 fHMTrans.SetBuffer3D(buffer);
127 buffer.SetSectionsValid(TBuffer3D::kCore);
129 // We fill kCore on first pass and try with viewer
130 Int_t reqSections = gPad->GetViewer3D()->AddObject(buffer);
131 if (reqSections == TBuffer3D::kNone) {
135 Error("TriangleSet::Paint", "only direct OpenGL rendering supported.");
138 /**************************************************************************/
142 TriangleSet* TriangleSet::ReadTrivialFile(const char* file)
144 FILE* f = fopen(file, "r");
146 ::Error("TriangleSet::ReadTrivialFile", Form("file '%s' not found.", file));
151 fscanf(f, "%d %d", &nv, &nt);
153 TriangleSet* ts = new TriangleSet(nv, nt);
155 Float_t *V = ts->Vertex(0);
156 for (Int_t i=0; i<nv; ++i) {
157 fscanf(f, "%f %f %f", V++, V++, V++);
160 Int_t *T = ts->Triangle(0);
161 for (Int_t i=0; i<nt; ++i) {
162 fscanf(f, "%d %d %d", T++, T++, T++);