1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
19 #include "TriangleSet.h"
20 #include "RGBAPalette.h"
25 #include <TVirtualPad.h>
26 #include <TVirtualViewer3D.h>
27 #include <TBuffer3D.h>
28 #include <TBuffer3DTypes.h>
32 //______________________________________________________________________
35 // Made from a list of vertices and a list of triangles (triplets of
38 // If input is composed from triangles with direct vertex coordinates
39 // one should consider finding all occurences of the same vertex
40 // and specifying it only once.
44 TriangleSet::TriangleSet(Int_t nv, Int_t nt, Bool_t norms, Bool_t cols) :
45 RenderElement(fColor),
46 TNamed("TriangleSet", 0),
47 fNVerts (nv), fVerts(0),
48 fNTrings (nt), fTrings(0), fTringNorms(0), fTringCols(0),
52 fVerts = new Float_t[3*fNVerts];
53 fTrings = new Int_t [3*fNTrings];
54 fTringNorms = (norms) ? new Float_t[3*fNTrings] : 0;
55 fTringCols = (cols) ? new UChar_t[3*fNTrings] : 0;
58 TriangleSet::~TriangleSet()
62 delete [] fTringNorms;
66 /**************************************************************************/
68 void TriangleSet::GenerateTriangleNormals()
70 if (fTringNorms == 0) fTringNorms = new Float_t[3*fNTrings];
73 Float_t *N = fTringNorms;
75 for(Int_t t=0; t<fNTrings; ++t, N+=3, T+=3)
77 Float_t* v0 = Vertex(T[0]);
78 Float_t* v1 = Vertex(T[1]);
79 Float_t* v2 = Vertex(T[2]);
80 e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]);
81 e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]);
88 void TriangleSet::GenerateRandomColors()
90 if (fTringCols == 0) fTringCols = new UChar_t[3*fNTrings];
94 UChar_t *C = fTringCols;
95 for(Int_t t=0; t<fNTrings; ++t, C+=3)
97 C[0] = (UChar_t) r.Uniform(60, 255);
98 C[1] = (UChar_t) r.Uniform(60, 255);
99 C[2] = (UChar_t) r.Uniform(60, 255);
103 void TriangleSet::GenerateZNormalColors(Float_t fac, Int_t min, Int_t max,
104 Bool_t interp, Bool_t wrap)
106 if (fTringCols == 0) fTringCols = new UChar_t[3*fNTrings];
107 if (fTringNorms == 0) GenerateTriangleNormals();
109 RGBAPalette pal(min, max, interp, wrap);
110 UChar_t *C = fTringCols;
111 Float_t *N = fTringNorms;
112 for(Int_t t=0; t<fNTrings; ++t, C+=3, N+=3)
114 Int_t v = TMath::Nint(fac * N[2]);
115 pal.ColorFromValue(v, C, kFALSE);
117 gPad->Modified(); gPad->Update();
120 /**************************************************************************/
122 void TriangleSet::ComputeBBox()
131 for (Int_t i=0; i<fNVerts; ++i, v += 3)
135 void TriangleSet::Paint(Option_t* )
137 TBuffer3D buffer(TBuffer3DTypes::kGeneric);
141 buffer.fColor = fColor;
142 buffer.fTransparency = 0;
143 fHMTrans.SetBuffer3D(buffer);
144 buffer.SetSectionsValid(TBuffer3D::kCore);
146 // We fill kCore on first pass and try with viewer
147 Int_t reqSections = gPad->GetViewer3D()->AddObject(buffer);
148 if (reqSections == TBuffer3D::kNone) {
152 Error("TriangleSet::Paint", "only direct OpenGL rendering supported.");
155 /**************************************************************************/
159 TriangleSet* TriangleSet::ReadTrivialFile(const char* file)
161 FILE* f = fopen(file, "r");
163 ::Error("TriangleSet::ReadTrivialFile", Form("file '%s' not found.", file));
168 fscanf(f, "%d %d", &nv, &nt);
170 TriangleSet* ts = new TriangleSet(nv, nt);
172 Float_t *V = ts->Vertex(0);
173 for (Int_t i=0; i<nv; ++i) {
174 fscanf(f, "%f %f %f", V++, V++, V++);
177 Int_t *T = ts->Triangle(0);
178 for (Int_t i=0; i<nt; ++i) {
179 fscanf(f, "%d %d %d", T++, T++, T++);