Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Primitives.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <OpenGLFramework/Utils/Primitives.hh>
26 #include <OpenGLFramework/Base/glfVertexFormat.hh>
27 
28 using namespace BIAS;
29 using namespace std;
30 
31 #define VERTEX2D 2
32 #define VERTEX3D 3
33 #define VERTEX4D 4
34 
35 #define TEXTURE2D 2
36 
37 
38 void Primitives::
39 AddRelativeQuadPatchOverImageLine(const unsigned int referenceWidth,
40  const unsigned int referenceHeight,
41  const unsigned int linePos,
42  const unsigned int hw,
44  const bool flip)
45 {
46 
47 
48  const unsigned int numPatches = 1;
49 
50  glfVertexFormat vertexFrmt;
51  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX2D);
52  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 0, GL_FLOAT, TEXTURE2D);
53  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 1, GL_FLOAT, TEXTURE2D);
54 
55  unsigned int numVertices = 4 * numPatches;
56  float* vertexData = new float[numVertices * (VERTEX2D + 2* TEXTURE2D) ]; // four vertices a two pos. coords and two times two texture coords
57 
58 
59 
60 
61  float W = static_cast<float>(referenceWidth);
62  float H = static_cast<float>(referenceHeight);
63 
64  // cout<<"W x H = "<<W<<"x"<<H<<endl;
65 
66  //here is no flip since we upload a flipped image, and render flipped then ?!a
67  float scaleToSpace[2] = { 2.0f/W, 2.0f/H}; //mx, my
68  float shiftToSpace[2] = {1.0f/W - 1.0f, 1.0f/H-1.0f};//px, py
69 // cout<<"scaleToSpace = "<<scaleToSpace[0]<<", "<<scaleToSpace[1]<<endl;
70 // cout<<"shiftToSpace = "<<shiftToSpace[0]<<", "<<shiftToSpace[1]<<endl;
71 
72  float scaleToTex[2] = {1.0f/W, -1.0f/H}; //ms, mt
73  float shiftToTex[2] = {1.0f/(2.0f*W), 1.0f - 1.0f/(2.0f*H)};//ps, pt
74 
75  float scaleToTexFlipped[2] = {1.0f/W, 1.0f/H};
76  float shiftToTexFlipped[2] = {1.0f/(2.0f*W), 1.0f/(2.0f*H)};
77 
78 
79  unsigned int offset=0;
80  for(unsigned int p=0; p<numPatches; p++) {
81  int ul[2] = {0, static_cast<int>(linePos)-static_cast<int>(hw)};
82  int lr[2] = {static_cast<int>(referenceWidth)-1, static_cast<int>(linePos)+static_cast<int>(hw)};
83 // cout<<lr[0]<<", "<<lr[1]<<endl;
84 // cout<<ul[0]<<", "<<ul[1]<<endl;
85  AddRelativeQuadPatch_(scaleToSpace, shiftToSpace,
86  scaleToTex, shiftToTex,
87  scaleToTexFlipped, shiftToTexFlipped,
88  ul, lr,
89  vertexData, offset, !flip);
90  }
91 
92 
93 
94  vb.Create(numVertices, vertexFrmt, vertexData);
95  delete [] vertexData;
96 
97  GLuint* indices = new GLuint[numVertices];
98  for(unsigned int i=0; i<numVertices; i++) indices[i] = i;
99  eb.Create(numVertices, GL_UNSIGNED_INT, GL_QUADS, indices);
100  delete [] indices;
101 
102 }
103 
104 
105 
106 void Primitives::
107 AddRelativeQuadPatches(const unsigned int referenceWidth,
108  const unsigned int referenceHeight,
109  const std::vector<unsigned int>& posX,
110  const std::vector<unsigned int>& posY,
111  const unsigned int hw,
113  const bool flip)
114 {
115 
116 
117  unsigned int numPatches = posX.size();
118  BIASASSERT(numPatches == posY.size());
119 
120  glfVertexFormat vertexFrmt;
121  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX2D);
122  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 0, GL_FLOAT, TEXTURE2D);
123  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 1, GL_FLOAT, TEXTURE2D);
124 
125  unsigned int numVertices = 4 * numPatches;
126  float* vertexData = new float[numVertices * (VERTEX2D + 2* TEXTURE2D) ]; // four vertices a two pos. coords and two times two texture coords
127 
128 
129 
130 
131  float W = static_cast<float>(referenceWidth);
132  float H = static_cast<float>(referenceHeight);
133 
134  // cout<<"W x H = "<<W<<"x"<<H<<endl;
135 
136  //here is no flip since we upload a flipped image, and render flipped then ?!a
137  float scaleToSpace[2] = { 2.0f/W, 2.0f/H}; //mx, my
138  float shiftToSpace[2] = {1.0f/W - 1.0f, 1.0f/H-1.0f};//px, py
139 // cout<<"scaleToSpace = "<<scaleToSpace[0]<<", "<<scaleToSpace[1]<<endl;
140 // cout<<"shiftToSpace = "<<shiftToSpace[0]<<", "<<shiftToSpace[1]<<endl;
141 
142  float scaleToTex[2] = {1.0f/W, -1.0f/H}; //ms, mt
143  float shiftToTex[2] = {1.0f/(2.0f*W), 1.0f - 1.0f/(2.0f*H)};//ps, pt
144 
145  float scaleToTexFlipped[2] = {1.0f/W, 1.0f/H};
146  float shiftToTexFlipped[2] = {1.0f/(2.0f*W), 1.0f/(2.0f*H)};
147 
148 
149  unsigned int offset=0;
150  for(unsigned int p=0; p<numPatches; p++) {
151  int ul[2] = {static_cast<int>(posX[p])-static_cast<int>(hw),
152  static_cast<int>(posY[p])-static_cast<int>(hw)};
153  int a = posX[p]+hw,b = posY[p]+hw;
154  int lr[2] = {a,b};
155 // cout<<lr[0]<<", "<<lr[1]<<endl;
156 // cout<<ul[0]<<", "<<ul[1]<<endl;
157  AddRelativeQuadPatch_(scaleToSpace, shiftToSpace,
158  scaleToTex, shiftToTex,
159  scaleToTexFlipped, shiftToTexFlipped,
160  ul, lr,
161  vertexData, offset, !flip);
162  }
163 
164 
165 
166  vb.Create(numVertices, vertexFrmt, vertexData);
167  delete [] vertexData;
168 
169  GLuint* indices = new GLuint[numVertices];
170  for(unsigned int i=0; i<numVertices; i++) indices[i] = i;
171  eb.Create(numVertices, GL_UNSIGNED_INT, GL_QUADS, indices);
172  delete [] indices;
173 
174 }
175 
176 
177 void Primitives::
178 AddRelativeQuadPatch_(const float scaleToSpace[2],
179  const float shiftToSpace[2],
180  const float scaleToTex[2],
181  const float shiftToTex[2],
182  const float scaleToTexFlipped[2],
183  const float shiftToTexFlipped[2],
184  const int ul[2],
185  const int lr[2],//included
186  float* supplementedArray,
187  unsigned int& offset,//incremented while adding
188  const bool flip)
189 {
190 
191  float spaceUL[2];
192  float spaceLR[2];
193  float texUL[2];
194  float texLR[2];
195  float texFlippedUL[2];
196  float texFlippedLR[2];
197  float tmpTexFlippedUL[2];
198  float tmpTexFlippedLR[2];
199 
200 
201 // cout<<"scaleToSpace = "<<scaleToSpace[0]<<", "<<scaleToSpace[1]<<endl;
202 // cout<<"shiftToSpace = "<<shiftToSpace[0]<<", "<<shiftToSpace[1]<<endl;
203 
204  for(unsigned int i=0; i<2; i++) {
205  spaceUL[i] = scaleToSpace[i]*(static_cast<float>(ul[i])-0.5)+shiftToSpace[i];
206  spaceLR[i] = scaleToSpace[i]*(static_cast<float>(lr[i])+0.5)+shiftToSpace[i];
207  texUL[i] = scaleToTex[i]*(static_cast<float>(ul[i])-0.5)+shiftToTex[i];
208  texLR[i] = scaleToTex[i]*(static_cast<float>(lr[i])+0.5)+shiftToTex[i];
209  tmpTexFlippedUL[i] = scaleToTexFlipped[i]*(static_cast<float>(ul[i])-0.5)+shiftToTexFlipped[i];
210  tmpTexFlippedLR[i] = scaleToTexFlipped[i]*(static_cast<float>(lr[i])+0.5)+shiftToTexFlipped[i];
211  if(!flip) {
212  texFlippedUL[i] = tmpTexFlippedUL[i];
213  texFlippedLR[i] = tmpTexFlippedLR[i];
214  } else {
215  texFlippedUL[i] = texUL[i];
216  texFlippedLR[i] = texLR[i];
217  texUL[i] = tmpTexFlippedUL[i];
218  texLR[i] = tmpTexFlippedLR[i];
219  }
220  }
221 
222  /* GL_QUADS
223  * 3--2
224  * | |
225  * 0--1
226  */
227  //0
228  supplementedArray[offset++] = spaceUL[0];
229  supplementedArray[offset++] = spaceLR[1];
230 // cout<<spaceUL[0]<<","<<spaceLR[1]<<endl;
231  supplementedArray[offset++] = texUL[0];
232  supplementedArray[offset++] = texLR[1];
233  supplementedArray[offset++] = texFlippedUL[0];
234  supplementedArray[offset++] = texFlippedLR[1];
235  //1 is LR
236  supplementedArray[offset++] = spaceLR[0];
237  supplementedArray[offset++] = spaceLR[1];
238 // cout<<spaceLR[0]<<","<<spaceLR[1]<<endl;
239  supplementedArray[offset++] = texLR[0];
240  supplementedArray[offset++] = texLR[1];
241  supplementedArray[offset++] = texFlippedLR[0];
242  supplementedArray[offset++] = texFlippedLR[1];
243  //2
244  supplementedArray[offset++] = spaceLR[0];
245  supplementedArray[offset++] = spaceUL[1];
246 // cout<<spaceLR[0]<<","<<spaceUL[1]<<endl;
247  supplementedArray[offset++] = texLR[0];
248  supplementedArray[offset++] = texUL[1];
249  supplementedArray[offset++] = texFlippedLR[0];
250  supplementedArray[offset++] = texFlippedUL[1];
251  //23 is UL
252  supplementedArray[offset++] = spaceUL[0];
253  supplementedArray[offset++] = spaceUL[1];
254 // cout<<spaceUL[0]<<","<<spaceUL[1]<<endl;
255  supplementedArray[offset++] = texUL[0];
256  supplementedArray[offset++] = texUL[1];
257  supplementedArray[offset++] = texFlippedUL[0];
258  supplementedArray[offset++] = texFlippedUL[1];
259 }
260 
261 
262 void Primitives::
264 {
265  glfVertexFormat vertexFrmt;
266  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX2D);
267  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 0, GL_FLOAT, TEXTURE2D);
268  vertexFrmt.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 1, GL_FLOAT, TEXTURE2D);
269 
270  float* vertexData = new float[4 * (VERTEX2D + 2* TEXTURE2D) ]; // four vertices a two pos. coords and two times two texture coords
271  AddPlain2DQuadVertices(vertexData, 0, 4);
272  AddPlain2DTextureCoordinates(vertexData, 2, 4, flip);
273  AddPlain2DTextureCoordinates(vertexData, 4, 4, !flip);
274 
275  vb.Create(4, vertexFrmt, vertexData);
276  delete [] vertexData;
277 
278  GLuint* indices = new GLuint[4];
279  for(unsigned int i=0; i<4; i++) indices[i] = i;
280  eb.Create(4, GL_UNSIGNED_INT, GL_QUADS, indices);
281  delete [] indices;
282 
283 }
284 
285 
286 void Primitives::
287 AddPlain2DQuadVertices(float* supplementedArray, unsigned int offset, unsigned int stride)
288 {
289  /* GL_QUADS
290  * 3--2
291  * | |
292  * 0--1
293  */
294 
295  unsigned int i=offset;
296  supplementedArray[i++] = -1.0;
297  supplementedArray[i++] = -1.0; //vertex
298  i+=stride;
299 
300  supplementedArray[i++] = 1.0;
301  supplementedArray[i++] = -1.0; //vertex
302  i+=stride;
303 
304  supplementedArray[i++] = 1.0;
305  supplementedArray[i++] = 1.0; //vertex
306  i+=stride;
307 
308  supplementedArray[i++] = -1.0;
309  supplementedArray[i++] = 1.0; //vertex
310 
311 }
312 
313 
314 void Primitives::
315 AddPlain2DTextureCoordinates(float* supplementedArray, unsigned int offset, unsigned int stride, bool flip)
316 {
317 /* GL_QUADS
318  * 3--2
319  * | |
320  * 0--1
321  */
322 
323  unsigned int i=offset;
324  supplementedArray[i++] = 0.0;
325  supplementedArray[i++] = (!flip)? 0.0 : 1.0; //tex
326  i+=stride;
327 
328  supplementedArray[i++] = 1.0;
329  supplementedArray[i++] = (!flip)? 0.0 : 1.0; //tex
330  i+=stride;
331 
332  supplementedArray[i++] = 1.0;
333  supplementedArray[i++] = (!flip)? 1.0 : 0.0; //tex
334  i+=stride;
335 
336  supplementedArray[i++] = 0.0;
337  supplementedArray[i++] = (!flip)? 1.0 : 0.0; //tex
338 
339 
340 }
341 
342 void Primitives::
345 {
346 
347  unsigned int width, height;
348  ppp.GetImageSize(width, height);
349 
350  glfVertexFormat vertexFormat;
351  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX4D);
352 
353  float* vertexData = new float [4 * VERTEX4D];//4 vertices a 4 components
354 
355  HomgPoint2D imgPoint;
356  enum {ul =0, ll, lr, ur};//names relate to the 3D position in cam coordinates.
357  Vector3<double> ray[4], pos;
358 
359  imgPoint.Set(-0.5, -0.5);
360  ppp.UnProjectLocal(imgPoint, pos, ray[ul]);
361  ray[ul] /= ray[ul][2];
362 
363  imgPoint.Set(-0.5, float(height)-0.5);
364  ppp.UnProjectLocal(imgPoint, pos, ray[ll]);
365  ray[ll] /= ray[ll][2];
366 
367  imgPoint.Set(float(width)-0.5, float(height)-0.5);
368  ppp.UnProjectLocal(imgPoint, pos, ray[lr]);
369  ray[lr] /= ray[lr][2];
370 
371  imgPoint.Set(float(width)-0.5, -0.5);
372  ppp.UnProjectLocal(imgPoint, pos, ray[ur]);
373  ray[ur] /= ray[ur][2];
374 
375  unsigned int count = 0;
376  Vector4<double> in, out;
377  for(unsigned int i =0; i< 4; i++) {
378  /*cout<<"\n\n v"<<i<<" = "<<ray[i]<<endl;*/
379  vertexData[count++] =
380  static_cast<float>(ray[i][0]);
381  vertexData[count++] =
382  static_cast<float>(ray[i][1]);
383  vertexData[count++] =
384  static_cast<float>(ray[i][2]);
385  vertexData[count++] = 1.0;
386 
387  }
388 
389  vb.Create(4, vertexFormat, vertexData);
390  delete [] vertexData;
391 
392  GLuint* indices = new GLuint[4];
393  for(unsigned int i=0; i<4; i++) indices[i] = i;
394  eb.Create(4, GL_UNSIGNED_INT, GL_QUADS, indices);
395  delete [] indices;
396 
397 
398 }
399 
400 void Primitives::
402  const std::vector<unsigned int>& posX,
403  const std::vector<unsigned int>& posY,
404  const unsigned int hw,
406 {
407 
408  unsigned int numPatches = posX.size();
409  BIASASSERT(numPatches == posY.size());
410 
411 
412  unsigned int width, height;
413  ppp.GetImageSize(width, height);
414 
415  glfVertexFormat vertexFormat;
416  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX4D);
417 
418  float* vertexData = new float [numPatches * 4 * VERTEX4D];//4 vertices a 4 components for each patch.
419 
420  HomgPoint2D imgPoint;
421  enum {ul =0, ll, lr, ur};//names relate to the 3D position in cam coordinates.
422  Vector3<double> ray[4], pos;
423 
424  unsigned int count = 0;
425  for(unsigned int p=0; p<numPatches; p++) {
426 
427  //find the four corners of the patch
428 
429  imgPoint.Set(posX[p]-hw-0.5, posY[p]-hw-0.5);
430  ppp.UnProjectLocal(imgPoint, pos, ray[ul]);
431 
432  imgPoint.Set(posX[p]-hw-0.5, posY[p]+hw+0.5);
433  ppp.UnProjectLocal(imgPoint, pos, ray[ll]);
434 
435  imgPoint.Set(posX[p]+hw+0.5, posY[p]+hw+0.5);
436  ppp.UnProjectLocal(imgPoint, pos, ray[lr]);
437 
438  imgPoint.Set(posX[p]+hw+0.5, posY[p]-hw-0.5);
439  ppp.UnProjectLocal(imgPoint, pos, ray[ur]);
440 
441 
442  Vector4<double> in, out;
443  for(unsigned int i =0; i< 4; i++) {
444  /*cout<<"\n\n v"<<i<<" = "<<ray[i]<<endl;*/
445  vertexData[count++] =
446  static_cast<float>(ray[i][0]);
447  vertexData[count++] =
448  static_cast<float>(ray[i][1]);
449  vertexData[count++] =
450  static_cast<float>(ray[i][2]);
451  vertexData[count++] = 1.0;
452 
453  }
454  }
455  vb.Create(4 * numPatches, vertexFormat, vertexData);
456  delete [] vertexData;
457 
458  GLuint* indices = new GLuint[4 * numPatches];
459  for(unsigned int i=0; i< 4 * numPatches; i++) indices[i] = i;
460  eb.Create(4*numPatches, GL_UNSIGNED_INT, GL_QUADS, indices);
461  delete [] indices;
462 
463 
464 }
465 
466 void Primitives::
468  const unsigned int posY,
469  const unsigned int hw,
471 {
472 
473  unsigned int numPatches = 1;
474 
475 
476  unsigned int width, height;
477  ppp.GetImageSize(width, height);
478 
479  glfVertexFormat vertexFormat;
480  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX4D);
481 
482  float* vertexData = new float [numPatches * 4 * VERTEX4D];//4 vertices a 4 components for each patch.
483 
484  HomgPoint2D imgPoint;
485  enum {ul =0, ll, lr, ur};//names relate to the 3D position in cam coordinates.
486  Vector3<double> ray[4], pos;
487 
488  unsigned int count = 0;
489  //for(unsigned int p=0; p<numPatches; p++) {
490 
491  //find the four corners of the patch
492 
493  imgPoint.Set(-0.5, posY-hw-0.5);
494  ppp.UnProjectLocal(imgPoint, pos, ray[ul]);
495 
496  imgPoint.Set(-0.5, posY+hw+0.5);
497  ppp.UnProjectLocal(imgPoint, pos, ray[ll]);
498 
499  imgPoint.Set(width-0.5, posY+hw+0.5);
500  ppp.UnProjectLocal(imgPoint, pos, ray[lr]);
501 
502  imgPoint.Set(width-0.5, posY-hw-0.5);
503  ppp.UnProjectLocal(imgPoint, pos, ray[ur]);
504 
505 
506  Vector4<double> in, out;
507  for(unsigned int i =0; i< 4; i++) {
508  /*cout<<"\n\n v"<<i<<" = "<<ray[i]<<endl;*/
509  vertexData[count++] =
510  static_cast<float>(ray[i][0]);
511  vertexData[count++] =
512  static_cast<float>(ray[i][1]);
513  vertexData[count++] =
514  static_cast<float>(ray[i][2]);
515  vertexData[count++] = 1.0;
516 
517  }
518  //}
519  vb.Create(4 * numPatches, vertexFormat, vertexData);
520  delete [] vertexData;
521 
522  GLuint* indices = new GLuint[4 * numPatches];
523  for(unsigned int i=0; i< 4 * numPatches; i++) indices[i] = i;
524  eb.Create(4*numPatches, GL_UNSIGNED_INT, GL_QUADS, indices);
525  delete [] indices;
526 
527 
528 }
529 
530 
531 void Primitives::
533  glfVertexBuffer& vb,
534  bool normalizeZ, bool invertS)
535 {
536  unsigned int width, height;
537  ppp.GetImageSize(width, height);
538 
539  glfVertexFormat vertexFormat;
540  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, VERTEX3D);
541  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_TEXCOORD, 0, GL_FLOAT, TEXTURE2D);
542 
543  float* vertexData = new float[width*height * (VERTEX3D + TEXTURE2D)];
544 
545  unsigned int pos = 0;
546  for(unsigned int y=0; y<height; y++) {
547  for(unsigned int x=0; x<width; x++) {
548  //calculate vertex
549  Vector3<double> dir, p;
550  ppp.UnProjectLocal(HomgPoint2D(x,y), p, dir);
551 
552  if(normalizeZ)
553  dir /= dir[2];
554 
555  vertexData[pos++] = static_cast<float>(dir[0]);
556  vertexData[pos++] = static_cast<float>(dir[1]);
557  vertexData[pos++] = static_cast<float>(dir[2]);
558 
559  //calculate texture coord
560  vertexData[pos++] = (static_cast<float>(x)+0.5)/static_cast<float>(width);
561  if(invertS) {
562  vertexData[pos++] = 1.0 - (static_cast<float>(y)+0.5)/static_cast<float>(height);
563  } else {
564  vertexData[pos++] = (static_cast<float>(y)+0.5)/static_cast<float>(height);
565  }
566  }
567  }
568 
569 
570  vb.Create(width*height, vertexFormat, vertexData);
571  delete [] vertexData;
572 }
573 
574 
575 
576 void Primitives::
578  glfVertexBuffer& vb, glfElementBuffer& eb, bool normalizeZ, bool invertS)
579 {
580 
581  unsigned int width, height;
582  ppp.GetImageSize(width, height);
583 
584  LocalNormalizedVertexCloud(ppp, vb, normalizeZ, invertS);
585 
586  GLuint* indices = new GLuint[width*height];
587  for(unsigned int i=0; i<width*height; i++) indices[i] = i;
588  eb.Create(width*height, GL_UNSIGNED_INT, GL_POINTS, indices);
589  delete [] indices;
590 
591 
592 }
593 
594 void Primitives::
597  bool normalizeZ)
598 {
599  unsigned int width, height;
600  pppSource.GetImageSize( width, height);
601 
602  LocalNormalizedVertexCloud(pppSource, vb, normalizeZ, true);
603 
604  //************** INDICES ***********************
605  //creating coordinates for triangle strips!
606  //each image rows, except for the first and last, are contained twice
607  //a triangle strip consists of 2*width vertices
608  // and height-1 strips have to be rendered
609  GLuint* triangleIndices =
610  new GLuint [width*(height-1)*2];
611  unsigned int numTriangleIndex = 0;
612  //
613  for(unsigned int y=0; y<height-1; y++) {
614  for(unsigned int x=0; x<width; x++) {
615  /* v1
616  * i0--- i2
617  * | / |
618  * | / |
619  * i1 i3
620  */
621  triangleIndices[numTriangleIndex++] = y*width+x;//up
622  triangleIndices[numTriangleIndex++] = (y+1)*width+x;//below
623  }
624  }
625  eb.Create(width*(height-1)*2,
626  GL_UNSIGNED_INT,
627  GL_TRIANGLE_STRIP,
628  triangleIndices);
629 
630  delete [] triangleIndices;
631 }
632 
633 
634 void Primitives::
637  bool normalizeZ, bool invertS)
638 {
639  unsigned int width, height;
640  pppSource.GetImageSize( width, height);
641 
642  LocalNormalizedVertexCloud(pppSource, vb, normalizeZ, invertS);
643 
644  //************** INDICES ***********************
645  /* Type A Type B
646  * i0--- i2 i0
647  * | / / |
648  * | / / |
649  * i1 i1-- i2
650  */
651  /*
652  * Assume that each image points belongs to 6 triangles : width*height*6
653  * two corners belong to only one triangle : - 2*6+2 = -2*5 = -10
654  * two corners belong to two triangles : - 2*6+2*2 = -2*4 = -8
655  * edges (without corners) belong to 3 triangles : - 2*(width-2+height-2)*6 + 2*(width-2+height-2)*3
656  * = -2*(width-2+height-2)*3
657  * = -(width+height-4)*6 = -(width+height)*6 + 24
658  * sum = (width*height)*6 - (width + height)*6 + 6 = (width*height - width - height + 1)*6
659  *
660  */
661  const int numIndices = (width*height - width - height + 1)*6;
662  GLuint* triangleIndices = new GLuint [numIndices];
663 
664  unsigned int cIdx = 0;//current element array position
665  for(unsigned int y=0; y<height-1; y++) {
666  for(unsigned int x=0; x<width-1; x++) {
667  //build Type A
668  triangleIndices[cIdx++] = y*width+x; // i0
669  triangleIndices[cIdx++] =(y+1)*width+x; // i1
670  triangleIndices[cIdx++] = y * width+x+1;// i2
671 
672  //build TypeB
673  triangleIndices[cIdx++] = y*width+ x+1; // i0
674  triangleIndices[cIdx++] =(y+1)*width+ x; // i1
675  triangleIndices[cIdx++] =(y+1)*width+ x+1;// i2
676 
677  }
678  }
679  eb.Create(numIndices,
680  GL_UNSIGNED_INT,
681  GL_TRIANGLES,
682  triangleIndices);
683 
684  delete [] triangleIndices;
685 }
686 
687 #ifdef GL_VERSION_3_0
688 void Primitives::
689 CalculateLocalRayField(const ProjectionParametersBase& ppb, glfVertexArrayObject& vao,
690  GLsizei& count,
691  GLint VertexCoordSlot, GLint TexCoordSlot, bool normalizeZ)
692 {
693 #ifdef BIAS_DEBUG
694  if(VertexCoordSlot != -1 || TexCoordSlot != -1) {
695  BIASASSERT(VertexCoordSlot!=TexCoordSlot)
696  }
697 #endif
698  count = 0;
699  if(VertexCoordSlot == -1 && TexCoordSlot == -1) {
700  BIASWARN("This function call has no effect!");
701  return;
702  }
703 
704  unsigned int width, height;
705  ppb.GetImageSize(width, height);
706  float* vertexData = (VertexCoordSlot != -1) ? new float[width*height*3] : NULL;
707  float* texCoordData = (TexCoordSlot != -1) ? new float[width*height*3] : NULL; // there will be two sets of texture coordinates which share the s component
708 
709 
710  unsigned int posVertexCoord = 0;
711  unsigned int posTexCoord = 0;
712  Vector3<double> pos, dir;
713  for(unsigned int y=0; y<height; y++) {
714  for(unsigned int x=0; x<width; x++) {
715  ppb.UnProjectLocal(HomgPoint2D(x,y), pos, dir);
716 
717  if(dir[0] == 0 && dir[1] == 0 && dir[2] == 0) continue;
718  if(normalizeZ) {
719  if(dir[2]==0) {
720  BIASERR("ray is valid but z-component is zero!");
721  BIASABORT;
722  }
723  BIASASSERT(dir[2]>0)
724  }
725  count++;
726  if(VertexCoordSlot != -1) {
727  if(normalizeZ)
728  dir /= dir[2];
729  vertexData[posVertexCoord++] = static_cast<float>(dir[0]);
730  vertexData[posVertexCoord++] = static_cast<float>(dir[1]);
731  vertexData[posVertexCoord++] = static_cast<float>(dir[2]);
732  }
733 
734  if(TexCoordSlot != -1) {
735  texCoordData[posTexCoord++] = (static_cast<float>(x)+0.5)/static_cast<float>(width);
736  texCoordData[posTexCoord++] = (static_cast<float>(y)+0.5)/static_cast<float>(height);
737  texCoordData[posTexCoord++] = 1.0 - (static_cast<float>(y)+0.5)/static_cast<float>(height);
738  }
739  }
740  }
741  unsigned int numValues = width*height;
742  if(VertexCoordSlot != -1)
743  vao.UploadData(VertexCoordSlot, 3, vertexData, numValues);
744  if(TexCoordSlot != -1)
745  vao.UploadData(TexCoordSlot, 3, texCoordData, numValues);
746 
747  delete [] vertexData;
748  delete [] texCoordData;
749 }
750 #endif
751 
static void LocalPerspectivePatchOverLine(const BIAS::ProjectionParametersPerspective &ppp, const unsigned int posY, const unsigned int hw, glfVertexBuffer &vb, glfElementBuffer &eb)
Patch encloses a region around a complete image line.
Definition: Primitives.cpp:467
static void LocalPerspectiveQuad(const BIAS::ProjectionParametersPerspective &ppp, BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb)
Definition: Primitives.cpp:343
An element buffer contains vertex indices that form primitives.
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
static void LocalNormalizedTriangleMesh(const BIAS::ProjectionParametersPerspective &pppSource, BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, bool normalizeZ, bool invertS)
Definition: Primitives.cpp:635
static void LocalNormalizedTriangleStrip(const BIAS::ProjectionParametersPerspective &pppSource, BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, bool normalizeZ=false)
Definition: Primitives.cpp:595
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
static void AddRelativeQuadPatchOverImageLine(const unsigned int referenceWidth, const unsigned int referenceHeight, const unsigned int linePos, const unsigned int hw, BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, const bool flip)
Adds a patch suited for rendering with identity rendering parameters that exactly fits over an image ...
Definition: Primitives.cpp:39
A vertex buffer contains an array of vertices that can be used for rendering.
void Create(int numVertices, const glfVertexFormat &format, const void *data=NULL, bool useBufferIfPossible=true)
Creates the vertex buffer for the given number of vertices and vertex format.
static void LocalNormalizedVertexCloud(const BIAS::ProjectionParametersPerspective &ppp, BIAS::glfVertexBuffer &vb, bool normalizeZ, bool invertS)
Generates a vertex list of textured 3d vertices.
Definition: Primitives.cpp:532
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
void AddAttribute(Attribute attrib, int index, GLenum type, int size, bool normalized=false)
Adds a vertex attribute to the format.
static void LocalPerspectivePatches(const BIAS::ProjectionParametersPerspective &ppp, const std::vector< unsigned int > &posX, const std::vector< unsigned int > &posY, const unsigned int hw, glfVertexBuffer &vb, glfElementBuffer &eb)
Definition: Primitives.cpp:401
static void AddPlain2DTextureCoordinates(float *supplementedArray, unsigned int offset, unsigned int stride, bool flip=false)
Vertex Order: 3–2 | | 0–1 .
Definition: Primitives.cpp:315
static void LocalNormalizedPointCloud(const BIAS::ProjectionParametersPerspective &ppp, BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, bool normalizeZ, bool invertS)
Definition: Primitives.cpp:577
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &origin, Vector3< double > &direction, bool ignoreDistortion=false) const =0
Calculates the view ray, which belongs to the given position on the image plane, in local coordinates...
static void PlainQuad2DWithTexture2D(BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, bool flip=false)
Vertex Order: 3–2 | | 0–1 .
Definition: Primitives.cpp:263
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
void Create(int numIndices, GLenum type, GLenum mode, const void *indices)
Creates the element buffer for the given number of indices.
A vertex format describes the attributes of a vertex.
static void AddRelativeQuadPatches(const unsigned int referenceWidth, const unsigned int referenceHeight, const std::vector< unsigned int > &posX, const std::vector< unsigned int > &posY, const unsigned int hw, BIAS::glfVertexBuffer &vb, BIAS::glfElementBuffer &eb, const bool flip)
Calculates 2D patches in the image plane suited for rendering with identity rendering parameters...
Definition: Primitives.cpp:107
virtual void UnProjectLocal(const HomgPoint2D &pos, Vector3< double > &pointOnRay, Vector3< double > &direction, bool IgnoreDistortion=false) const
calculates the viewing ray from the camera center (in the camera coordinate system) which belongs to ...
static void AddPlain2DQuadVertices(float *supplementedArray, unsigned int offset, unsigned int stride)
Vertex Order: 3–2 | | 0–1 .
Definition: Primitives.cpp:287
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void Set(const HOMGPOINT2D_TYPE &x, const HOMGPOINT2D_TYPE &y)
set elementwise with given 2 euclidean scalar values.
Definition: HomgPoint2D.hh:174
class BIASGeometryBase_EXPORT HomgPoint2D