]>
Commit | Line | Data |
---|---|---|
6196c437 | 1 | // $Header$ |
2 | ||
3 | #include "TriangleSet.h" | |
d54c094e | 4 | #include "RGBAPalette.h" |
6196c437 | 5 | |
6 | #include <TVector3.h> | |
7 | #include <TRandom3.h> | |
8 | #include <TVirtualPad.h> | |
9 | #include <TVirtualViewer3D.h> | |
10 | #include <TBuffer3D.h> | |
11 | #include <TBuffer3DTypes.h> | |
6196c437 | 12 | |
13 | using namespace Reve; | |
14 | ||
15 | ||
16 | ClassImp(TriangleSet) | |
17 | ||
18 | TriangleSet::TriangleSet(Int_t nv, Int_t nt, Bool_t norms, Bool_t cols) : | |
19 | RenderElement(fColor), | |
20 | TNamed("TriangleSet", 0), | |
3c67f72c | 21 | fNVerts (nv), fVerts(0), |
22 | fNTrings (nt), fTrings(0), fTringNorms(0), fTringCols(0), | |
23 | fColor (2), | |
24 | fHMTrans () | |
6196c437 | 25 | { |
26 | fVerts = new Float_t[3*fNVerts]; | |
27 | fTrings = new Int_t [3*fNTrings]; | |
28 | fTringNorms = (norms) ? new Float_t[3*fNTrings] : 0; | |
29 | fTringCols = (cols) ? new UChar_t[3*fNTrings] : 0; | |
30 | } | |
31 | ||
32 | TriangleSet::~TriangleSet() | |
33 | { | |
34 | delete [] fVerts; | |
35 | delete [] fTrings; | |
36 | delete [] fTringNorms; | |
37 | delete [] fTringCols; | |
38 | } | |
39 | ||
40 | /**************************************************************************/ | |
41 | ||
42 | void TriangleSet::GenerateTriangleNormals() | |
43 | { | |
44 | if (fTringNorms == 0) fTringNorms = new Float_t[3*fNTrings]; | |
45 | ||
46 | TVector3 e1, e2, n; | |
47 | Float_t *N = fTringNorms; | |
48 | Int_t *T = fTrings; | |
49 | for(Int_t t=0; t<fNTrings; ++t, N+=3, T+=3) | |
50 | { | |
51 | Float_t* v0 = Vertex(T[0]); | |
52 | Float_t* v1 = Vertex(T[1]); | |
53 | Float_t* v2 = Vertex(T[2]); | |
54 | e1.SetXYZ(v1[0]-v0[0], v1[1]-v0[1], v1[2]-v0[2]); | |
55 | e2.SetXYZ(v2[0]-v0[0], v2[1]-v0[1], v2[2]-v0[2]); | |
56 | n = e1.Cross(e2); | |
57 | n.SetMag(1); | |
58 | n.GetXYZ(N); | |
59 | } | |
60 | } | |
61 | ||
62 | void TriangleSet::GenerateRandomColors() | |
63 | { | |
64 | if (fTringCols == 0) fTringCols = new UChar_t[3*fNTrings]; | |
65 | ||
66 | TRandom r; | |
67 | r.SetSeed(0); | |
68 | UChar_t *C = fTringCols; | |
69 | for(Int_t t=0; t<fNTrings; ++t, C+=3) | |
70 | { | |
71 | C[0] = (UChar_t) r.Uniform(60, 255); | |
72 | C[1] = (UChar_t) r.Uniform(60, 255); | |
73 | C[2] = (UChar_t) r.Uniform(60, 255); | |
74 | } | |
75 | } | |
76 | ||
64dae94d | 77 | void TriangleSet::GenerateZNormalColors(Float_t fac, Int_t min, Int_t max, |
78 | Bool_t interp, Bool_t wrap) | |
79 | { | |
80 | if (fTringCols == 0) fTringCols = new UChar_t[3*fNTrings]; | |
81 | if (fTringNorms == 0) GenerateTriangleNormals(); | |
82 | ||
d54c094e | 83 | RGBAPalette pal(min, max, interp, wrap); |
64dae94d | 84 | UChar_t *C = fTringCols; |
85 | Float_t *N = fTringNorms; | |
86 | for(Int_t t=0; t<fNTrings; ++t, C+=3, N+=3) | |
87 | { | |
88 | Int_t v = TMath::Nint(fac * N[2]); | |
89 | pal.ColorFromArray(v, C, kFALSE); | |
90 | } | |
91 | gPad->Modified(); gPad->Update(); | |
92 | } | |
93 | ||
6196c437 | 94 | /**************************************************************************/ |
95 | ||
96 | void TriangleSet::ComputeBBox() | |
97 | { | |
98 | if (fNVerts <= 0) { | |
99 | BBoxZero(); | |
100 | return; | |
101 | } | |
102 | ||
103 | BBoxInit(); | |
104 | Float_t* v = fVerts; | |
105 | for (Int_t i=0; i<fNVerts; ++i, v += 3) | |
106 | BBoxCheckPoint(v); | |
107 | } | |
108 | ||
109 | void TriangleSet::Paint(Option_t* ) | |
110 | { | |
111 | TBuffer3D buffer(TBuffer3DTypes::kGeneric); | |
112 | ||
113 | // Section kCore | |
114 | buffer.fID = this; | |
115 | buffer.fColor = fColor; | |
116 | buffer.fTransparency = 0; | |
117 | fHMTrans.SetBuffer3D(buffer); | |
118 | buffer.SetSectionsValid(TBuffer3D::kCore); | |
119 | ||
120 | // We fill kCore on first pass and try with viewer | |
121 | Int_t reqSections = gPad->GetViewer3D()->AddObject(buffer); | |
122 | if (reqSections == TBuffer3D::kNone) { | |
123 | return; | |
124 | } | |
125 | ||
126 | Error("TriangleSet::Paint", "only direct OpenGL rendering supported."); | |
127 | } | |
128 | ||
129 | /**************************************************************************/ | |
130 | ||
131 | #include <stdio.h> | |
132 | ||
133 | TriangleSet* TriangleSet::ReadTrivialFile(const char* file) | |
134 | { | |
135 | FILE* f = fopen(file, "r"); | |
8f40a17c | 136 | if (f == 0) { |
137 | ::Error("TriangleSet::ReadTrivialFile", Form("file '%s' not found.", file)); | |
138 | return 0; | |
139 | } | |
6196c437 | 140 | |
141 | Int_t nv, nt; | |
142 | fscanf(f, "%d %d", &nv, &nt); | |
143 | ||
144 | TriangleSet* ts = new TriangleSet(nv, nt); | |
145 | ||
146 | Float_t *V = ts->Vertex(0); | |
147 | for (Int_t i=0; i<nv; ++i) { | |
148 | fscanf(f, "%f %f %f", V++, V++, V++); | |
149 | } | |
150 | ||
151 | Int_t *T = ts->Triangle(0); | |
152 | for (Int_t i=0; i<nt; ++i) { | |
153 | fscanf(f, "%d %d %d", T++, T++, T++); | |
154 | } | |
155 | ||
156 | fclose(f); | |
157 | ||
158 | return ts; | |
159 | } |