source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Rendering/WrlFacestHroiReduce.cpp @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 18.8 KB
Line 
1/*
2% *  This code was used in the following articles:
3% *  [1] Learning 3-D Scene Structure from a Single Still Image,
4% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
5% *      In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
6% *      (best paper)
7% *  [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
8% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
9% *      In ICCV workshop on Virtual Representations and Modeling
10% *      of Large-scale environments (VRML), 2007.
11% *  [3] 3-D Depth Reconstruction from a Single Still Image,
12% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
13% *      International Journal of Computer Vision (IJCV), Aug 2007.
14% *  [6] Learning Depth from Single Monocular Images,
15% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
16% *      In Neural Information Processing Systems (NIPS) 18, 2005.
17% *
18% *  These articles are available at:
19% *  http://make3d.stanford.edu/publications
20% *
21% *  We request that you cite the papers [1], [3] and [6] in any of
22% *  your reports that uses this code.
23% *  Further, if you use the code in image3dstiching/ (multiple image version),
24% *  then please cite [2].
25% * 
26% *  If you use the code in third_party/, then PLEASE CITE and follow the
27% *  LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
28% *
29% *  Finally, this code is for non-commercial use only.  For further
30% *  information and to obtain a copy of the license, see
31% *
32% *  http://make3d.stanford.edu/publications/code
33% *
34% *  Also, the software distributed under the License is distributed on an
35% * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
36% *  express or implied.   See the License for the specific language governing
37% *  permissions and limitations under the License.
38% *
39% */
40
41#include <stdio.h>
42#include <cstdlib>
43#include <cstring>
44#include <math.h>
45#include "mex.h"
46
47/* Input Arguments */
48#define COORD3D_IN      prhs[0]
49#define COORDIm_IN      prhs[1]
50#define SUP             prhs[2]
51#define IMG_NAME        prhs[3]
52#define WRL_NAME        prhs[4]
53#define WRL_PATH        prhs[5]
54#define GRID            prhs[6]
55#define ATTACH            prhs[7]
56
57/* OutPUT Arguments */
58//#define WRL_NAME        plhs[0]
59
60/* Definition */
61#if !defined(BOU)
62#define BOU(A, B, C)       ((A) > (B) ? (C) : (A))
63#endif
64#if !defined(CheckMinus)
65#define CheckMinus(A, B)       ((A) == (-1) ? (B) : (A))
66#endif
67
68void WriteVrml( double ThreeDCoord[],
69                double ImCorrd[],
70                double Sup[],
71                char *ImgName,
72                char *wrlName,
73                char *WrlFolder,
74                char *WrlName,
75                unsigned int vr,
76                unsigned int hr,
77                int grid,
78                int attach)
79{
80   printf("grid %d",grid);
81   printf("attach %d",attach);
82 
83   int displayFlag = 0;
84
85 WrlName = "hi.wrl"; 
86/* int *BoundaryPt= new int(vr*hr);*/
87 int MaxV = (vr*hr); 
88 int *Index = new int[MaxV];
89 for (int i = 0; i < MaxV; i++){//Min Tried
90        Index[i] = -2; //Initialize to a unique value
91 }
92 int *ValidVertices= new int[MaxV];
93 int CPtr; //Index of the 2-D vr x hr map
94 
95 /* Identify the Boundary Point of each Superpixel*/
96 int IndCount = 0; /* Index for wrl start from 0*/
97 for (int k = 0; k < hr; k++){
98     CPtr = vr*k;
99     int CPtrMark = CPtr;
100     for (int i = 0; i < vr; i++){
101         int center = (int) Sup[CPtr];
102         if (center == 0 && i != (vr -1)){ // don't assign index to sky except the lowest row
103                Index[CPtr] = -1;//Min TRied
104         }
105         else if (
106             k != 0 && k != (hr-1) && /*excluding the boundary points*/
107             i != 0 && i != (vr-1) && /*excluding the boundary points*/
108             (int) Sup[ CPtr + 1] == center &&
109             (int) Sup[ CPtr - 1] == center &&
110             (int) Sup[ CPtr + vr] == center &&
111             (int) Sup[ CPtr - vr] == center){ //if boundary point down right, or 4 side same supindex, do not assign index
112         }
113         else{
114            int mark;
115            if (k != 0 && k != (hr-1) &&
116                i != 0 && // not for the top row
117                (int) Sup[ CPtr + vr] == center &&
118                (int) Sup[ CPtr - vr] == center){ // check left and right sup index if the same
119                mark = Index[ CPtr -vr];
120            }
121            else{
122                mark = IndCount;
123                ValidVertices[IndCount] = CPtr;
124                IndCount++;
125            }
126
127            /* start assigning Index*/
128            for ( int j = CPtrMark; j <= CPtr; j++){
129                if (Index[j] == -2){ //Min Tried
130                   Index[j] = mark;
131                }
132//                 printf("mark %d to %d",j,Index[j]); //print out index matrix element //Ashu
133                   CPtrMark++;
134            }
135//              printf("ValidVertices %d",CPtr);
136         }
137//          printf("CPtr %d \n",CPtr);
138         CPtr++;
139     }
140 }
141
142 /* test Index */
143/*   printf("\n");
144 for (int j = 0; j < CPtr; j++){
145   printf("Inde %d to %d",j,Index[j]); //should print out the same number as "mark" //Ashu
146 }*/
147 
148   if( displayFlag){
149        printf("Nu Index %d\n",CPtr);
150   }
151
152 
153 /* Draw Triangles */
154 FILE * fp;
155// fp = fopen( "RowColReducTri.wrl", "w");
156 char fullname[500000]; // big enought
157 fullname[0] = '\0';
158 strcat( fullname, WrlFolder ); 
159 strcat( fullname, wrlName ); 
160 strcat( fullname, ".wrl" );
161
162  if( displayFlag ) { 
163      printf("fullname = %s",fullname);
164  }
165 if (!attach){
166    fp = fopen( fullname , "w"); 
167    fprintf(fp, "#VRML V2.0 utf8\n");
168
169    /* % add navigate_info*/
170    fprintf(fp, "NavigationInfo {\n");
171    fprintf(fp, "  headlight TRUE\n");
172    fprintf(fp, "  type [\"FLY\", \"ANY\"]}\n\n");
173
174    /*% add viewpoint*/
175    fprintf(fp, "Viewpoint {\n");
176    fprintf(fp, "    position        0 0.0 0.0\n");
177    fprintf(fp, "    orientation     0 0 0 0\n");
178    fprintf(fp, "    fieldOfView     0.7\n");
179    fprintf(fp, "    description \"Original\"}\n");
180
181    /*%============== add background color======*/
182    fprintf(fp, "DEF Back1 Background {\n");
183    fprintf(fp, "groundColor [.3 .29 .27]\n");
184    fprintf(fp, "skyColor [0.31 0.54 0.76]}\n");
185    /*%=========================================*/
186 }else{
187    printf("a+");
188    fp = fopen( fullname , "a+"); 
189 }
190
191 
192
193 /*% add Shape for texture faceset*/
194 fprintf(fp, "Shape{\n");
195 fprintf(fp, "  appearance Appearance {\n");
196 fprintf(fp, "   texture ImageTexture { url \"./%s.jpg\" }\n",ImgName);
197 fprintf(fp, "  }\n");
198 fprintf(fp, "  geometry IndexedFaceSet {\n");
199 fprintf(fp, "    coord Coordinate {\n");
200
201 /*% insert coordinate in 3d*/
202 /*% =======================*/
203 fprintf(fp, "      point [ \n");
204
205 for (int i = 0; i < IndCount; i++){
206     fprintf(fp, "        %.2f %.2f %.2f,\n",
207             ThreeDCoord[ ValidVertices[i]],
208             ThreeDCoord[ ValidVertices[i] + MaxV],
209             ThreeDCoord[ ValidVertices[i] + MaxV + MaxV]
210              );
211 }
212 fprintf(fp, "      ]\n");
213 fprintf(fp, "    }\n");
214 
215 /*% insert coordinate index in 3d*/
216 fprintf(fp, "    coordIndex [\n");
217
218 for (int k = 0; k < (hr-1); k++){
219     int CPtr = vr*k;
220     for (int i = 0; i < (vr-1); i++){
221         if ( Sup[CPtr] == Sup[CPtr + vr + 1]){
222
223            /* Left Low Triangle*/
224            if ( Index[CPtr] == Index[CPtr+1] || 
225                 Index[CPtr] == Index[CPtr+1+vr] ||
226                 Index[CPtr + 1] == Index[CPtr+1+vr] ){ //if Index repeted in the triangle, do nothing
227            }
228            else{
229                if (Index[CPtr] != -1 &&
230                    Index[CPtr+1] != -1 &&
231                    Index[CPtr+1+vr] != -1){//Min Tried
232                       fprintf(fp, "              %d %d %d -1,\n",
233                               Index[CPtr],
234                               Index[CPtr+1],
235                                Index[CPtr+1+vr]
236                               );
237                }
238              }
239
240            /* Right Up Triangle*/
241            if ( Index[CPtr] == Index[CPtr+1+vr] ||
242                 Index[CPtr+vr] == Index[CPtr+1+vr] ||
243                 Index[CPtr+vr] == Index[CPtr]){
244//                 continue;
245            }
246            else{
247                if (Index[CPtr] != -1 &&
248                    Index[CPtr+1+vr] != -1 &&
249                    Index[CPtr+vr] != -1){ //Min Tried
250                       fprintf(fp, "              %d %d %d -1,\n",
251                               Index[CPtr],
252                               Index[CPtr+1+vr],
253                               Index[CPtr+vr]
254                       );
255                }
256            }
257
258
259         }
260         else{ //Sup[CPtr] != Sup[CPtr + vr + 1]
261   
262            /* Left Up Triangle*/
263            if ( Index[CPtr] == Index[CPtr+1] || 
264                 Index[CPtr] == Index[CPtr+vr] ||
265                 Index[CPtr + 1] == Index[CPtr+vr] ){
266            }
267            else{
268                if (Index[CPtr] != -1 &&
269                    Index[CPtr+1] != -1 &&
270                    Index[CPtr+vr] != -1){ //Min Tried
271                        fprintf(fp, "              %d %d %d -1,\n",
272                                Index[CPtr],
273                                Index[CPtr+1],
274                                Index[CPtr+vr]
275                        );
276                }
277            }
278
279            /* Right Low Triangle*/
280            if ( Index[CPtr+1] == Index[CPtr+1+vr] ||
281                 Index[CPtr+vr] == Index[CPtr+1+vr] ||
282                 Index[CPtr+vr] == Index[CPtr+1]){
283//                 continue;
284            }
285            else{
286                if (Index[CPtr+1] != -1 &&
287                    Index[CPtr+1+vr] != -1 &&
288                    Index[CPtr+vr] != -1){ //Min Tried
289                        fprintf(fp, "              %d %d %d -1,\n",
290                                Index[CPtr+1],
291                                Index[CPtr+1+vr],
292                                Index[CPtr+vr]
293                        );
294                }
295            }
296         }
297         CPtr++;
298     }
299 }
300 fprintf(fp, "    ]\n");
301
302 /*% insert texture coordinate*/
303 fprintf(fp, "    texCoord TextureCoordinate {\n");
304 fprintf(fp, "      point [\n");
305 
306for (int i = 0; i < IndCount; i++){
307     fprintf(fp, "              %.4g %.4g,\n",
308             ImCorrd[ ValidVertices[i]],
309             ImCorrd[ ValidVertices[i] + MaxV]
310              );
311 }
312 fprintf(fp, "        ]\n");
313 fprintf(fp, "    }\n");
314 fprintf(fp, "    texCoordIndex [\n");
315
316 int Tri =0;
317 for (int k = 0; k < (hr-1); k++){
318     int CPtr = vr*k;
319     for (int i = 0; i < (vr-1); i++){
320         if ( Sup[CPtr] == Sup[CPtr + vr + 1]){
321
322            /* Left Low Triangle*/
323            if ( Index[CPtr] == Index[CPtr+1] || 
324                 Index[CPtr] == Index[CPtr+1+vr] ||
325                 Index[CPtr + 1] == Index[CPtr+1+vr] ){
326//                 continue;
327            }
328            else{
329                if (Index[CPtr] != -1 &&
330                    Index[CPtr+1] != -1 &&
331                    Index[CPtr+1+vr] != -1){//Min Tried
332                        fprintf(fp, "              %d %d %d -1,\n",
333                                Index[CPtr],
334                                Index[CPtr+1],
335                                Index[CPtr+1+vr]
336                        );
337                        Tri++;
338                }
339            }
340//
341            /* Right Up Triangle*/
342            if ( Index[CPtr] == Index[CPtr+1+vr] ||
343                 Index[CPtr+vr] == Index[CPtr+1+vr] ||
344                 Index[CPtr+vr] == Index[CPtr]){
345//                 continue;
346            }
347            else{
348                if (Index[CPtr] != -1 &&
349                    Index[CPtr+1+vr] != -1&&
350                    Index[CPtr+vr] != -1){ //Min Tried
351                        fprintf(fp, "              %d %d %d -1,\n",
352                                Index[CPtr],
353                                Index[CPtr+1+vr],
354                                Index[CPtr+vr]
355                        );
356                        Tri++;   
357                }           
358            }
359
360         }
361         else{
362   
363            /* Left Up Triangle*/
364            if ( Index[CPtr] == Index[CPtr+1] || 
365                 Index[CPtr] == Index[CPtr+vr] ||
366                 Index[CPtr + 1] == Index[CPtr+vr] ){
367//                 continue;
368            }
369            else{
370                if (Index[CPtr] != -1 &&
371                    Index[CPtr+1] != -1 &&
372                    Index[CPtr+vr] != -1){ //Min Tried
373                        fprintf(fp, "              %d %d %d -1,\n",
374                                Index[CPtr],
375                                Index[CPtr+1],
376                                Index[CPtr+vr]
377                        );
378                        Tri++;     
379                }         
380            }
381
382            /* Right Low Triangle*/
383            if ( Index[CPtr+1] == Index[CPtr+1+vr] ||
384                 Index[CPtr+vr] == Index[CPtr+1+vr] ||
385                 Index[CPtr+vr] == Index[CPtr+1]){
386//                 continue;
387            }
388            else{
389                if (Index[CPtr+1] != -1 &&
390                    Index[CPtr+1+vr] != -1 &&
391                    Index[CPtr+vr] != -1){ //Min Tried
392                        fprintf(fp, "              %d %d %d -1,\n",
393                                Index[CPtr+1],
394                                Index[CPtr+1+vr],
395                                Index[CPtr+vr]
396                        );
397                        Tri++;         
398                }         
399            }
400         } //if ( Sup[CPtr] == Sup[CPtr + vr + 1])
401         CPtr++;
402     }
403 }
404 fprintf(fp, "    ]\n");
405 fprintf(fp, "  }\n");
406 fprintf(fp, "}\n");
407
408 /*write Sup*/
409 fprintf(fp, "#Sup [\n");
410 for (int i = 0; i < IndCount; i++){
411     fprintf(fp, "# %d,\n",
412             (int)Sup[ ValidVertices[i]]             
413              );
414 }
415
416 /* ==============drawing the grid ===================*/
417 if (grid) {
418    fprintf(fp, "Shape{\n");
419    fprintf(fp, "  appearance Appearance { material Material {emissiveColor 1 0 0  }}\n");
420    fprintf(fp, "    geometry IndexedLineSet {\n");
421    fprintf(fp, "    coord Coordinate {\n");
422    fprintf(fp, "      point [ \n");
423 
424    /*writing the 3D coordinate again*/
425    for (int i = 0; i < IndCount; i++){
426        fprintf(fp, "        %.2f %.2f %.2f,\n",
427        ThreeDCoord[ ValidVertices[i]],
428        ThreeDCoord[ ValidVertices[i] + MaxV],
429        ThreeDCoord[ ValidVertices[i] + MaxV + MaxV]
430        );
431    }
432    fprintf(fp, "      ]\n");
433    fprintf(fp, "    }\n");
434    fprintf(fp, "    coordIndex [\n");
435
436 /* write the index for the grid again*/
437 for (int k = 0; k < (hr-1); k++){
438     int CPtr = vr*k;
439     for (int i = 0; i < (vr-1); i++){
440         if ( Sup[CPtr] == Sup[CPtr + vr + 1]){
441
442            /* Left Low Triangle*/
443            if ( Index[CPtr] == Index[CPtr+1] || 
444                 Index[CPtr] == Index[CPtr+1+vr] ||
445                 Index[CPtr + 1] == Index[CPtr+1+vr] ){
446            }
447            else{
448               fprintf(fp, "              %d %d %d -1,\n",
449                       Index[CPtr],
450                       Index[CPtr+1],
451                       Index[CPtr+1+vr]
452                       );
453              }
454
455            /* Right Up Triangle*/
456            if ( Index[CPtr] == Index[CPtr+1+vr] ||
457                 Index[CPtr+vr] == Index[CPtr+1+vr] ||
458                 Index[CPtr+vr] == Index[CPtr]){
459//                 continue;
460            }
461            else{
462               fprintf(fp, "              %d %d %d -1,\n",
463                       Index[CPtr],
464                       Index[CPtr+1+vr],
465                       Index[CPtr+vr]
466                       );
467            }
468
469         }
470         else{
471   
472            /* Left Up Triangle*/
473            if ( Index[CPtr] == Index[CPtr+1] || 
474                 Index[CPtr] == Index[CPtr+vr] ||
475                 Index[CPtr + 1] == Index[CPtr+vr] ){
476            }
477            else{
478               fprintf(fp, "              %d %d %d -1,\n",
479                       Index[CPtr],
480                       Index[CPtr+1],
481                       Index[CPtr+vr]
482                       );
483            }
484
485            /* Right Low Triangle*/
486            if ( Index[CPtr+1] == Index[CPtr+1+vr] ||
487                 Index[CPtr+vr] == Index[CPtr+1+vr] ||
488                 Index[CPtr+vr] == Index[CPtr+1]){
489//                 continue;
490            }
491            else{
492               fprintf(fp, "              %d %d %d -1,\n",
493                       Index[CPtr+1],
494                       Index[CPtr+1+vr],
495                       Index[CPtr+vr]
496                       );
497            }
498         }
499         CPtr++;
500     }
501 }
502 /* =====================================================*/
503
504    fprintf(fp, "    ]\n");
505    fprintf(fp, "    colorPerVertex FALSE\n");
506    fprintf(fp, "    }\n");
507    fprintf(fp, "  }\n");
508    fprintf(fp, "}\n");
509 }
510
511 
512 fclose(fp);
513 printf("            In WRL, vertices=%d triangles=%d\n",IndCount,Tri);
514 delete [] Index;
515 delete [] ValidVertices;
516 return;
517}
518/* mexFunctin (matlab interface will be removed later) */
519void mexFunction( int nlhs, mxArray *plhs[],
520                  int nrhs, const mxArray *prhs[] )
521
522{
523    double *ThreeDCoord;
524    double *ImCorrd;
525    double *Sup;
526    double *grid;
527    double *attach;
528    char   *ImgName;
529    char   *wrlName;
530    char   *WrlFolder;
531    char   *WrlName;
532
533    unsigned int v,h,d,vr,hr;
534 
535   /* ****** WrlFacestHroiReduce( 3DPosition, ImgPosition, SupIndex, gridFlag, Attachflag)**** */
536
537    /* Check for proper number of arguments */
538
539    if (nrhs != 8) {
540        mexErrMsgTxt("Eight input arguments required.");
541    } else if (nlhs > 1) {
542        mexErrMsgTxt("Too many output arguments.");
543    }
544
545    /* Check the dimensions of the inputs. */
546
547    vr  = (mxGetDimensions(COORD3D_IN))[0];
548    hr  = (mxGetDimensions(COORD3D_IN))[1];
549    d  = (mxGetDimensions(COORD3D_IN))[2];
550    if (!mxIsDouble(COORD3D_IN) || mxIsComplex(COORD3D_IN) ||
551        ( d != 3)) {
552        mexErrMsgTxt("The depth of the 3DCoord Matrix should be 3.");
553    }
554    v  = (mxGetDimensions(COORDIm_IN))[0];
555    h  = (mxGetDimensions(COORDIm_IN))[1];
556    d  = (mxGetDimensions(COORDIm_IN))[2];
557    if (!mxIsDouble(COORDIm_IN) || mxIsComplex(COORDIm_IN) ||
558        ( d != 2) || ( v != vr) || (h != hr)) {
559        mexErrMsgTxt("The depth of the Image Coord Matrix should be 2.");
560    }
561    v = mxGetM(SUP);
562    h = mxGetN(SUP);
563    if (!mxIsDouble(SUP) || mxIsComplex(SUP) ||
564        (v != vr) || (h != hr)) {
565        mexErrMsgTxt("Superpixel is not having the same v and h with others.");
566    }
567    if (!mxIsChar(IMG_NAME) || mxIsComplex(IMG_NAME) ) {
568        mexErrMsgTxt("Image name is not a string.");
569    }
570    if (!mxIsChar(WRL_NAME) || mxIsComplex(WRL_NAME) ) {
571        mexErrMsgTxt("Image name is not a string.");
572    }
573    if (!mxIsChar(WRL_PATH) || mxIsComplex(WRL_PATH) ) {
574        mexErrMsgTxt("WRL path is not a string.");
575    }
576    v = mxGetM(GRID);
577    h = mxGetN(GRID);
578    if (!mxIsDouble(GRID) || mxIsComplex(GRID) ||
579        (v != 1) || (h != 1)) {
580        mexErrMsgTxt("GRID is not scalar.");
581    }
582    v = mxGetM(ATTACH);
583    h = mxGetN(ATTACH);
584    if (!mxIsDouble(ATTACH) || mxIsComplex(ATTACH) ||
585        (v != 1) || (h != 1)) {
586        mexErrMsgTxt("ATTACH is not scalar.");
587    }
588
589    /* Create a matrix for the return argument */
590    /*WRL_NAME = mxCreateCharMatrixFromStrings(1, mxREAL);
591    mxCreateCharArray(mwSize ndim, );*/
592
593    /* Assign pointers to the various parameters */
594    //WRL_NAME = mxCreateString(WrlName);
595
596    ThreeDCoord = mxGetPr(COORD3D_IN);
597    ImCorrd = mxGetPr(COORDIm_IN);
598    Sup =  mxGetPr(SUP);
599    ImgName = mxArrayToString(IMG_NAME);
600    wrlName = mxArrayToString(WRL_NAME);
601    WrlFolder = mxArrayToString(WRL_PATH);
602    grid =  mxGetPr(GRID);
603    attach =  mxGetPr(ATTACH);
604//    printf("WrlFolder %s",WrlFolder);
605
606    /* Do the actual computations in a subroutine */
607/*    WriteVrml( ThreeDCoord, ImCorrd, Sup, ImgName, WrlFolder, WrlName, vr, hr);*/
608    WriteVrml( ThreeDCoord, ImCorrd, Sup, ImgName, wrlName, WrlFolder, WrlName,
609               vr, hr, (int)*grid, (int)*attach);
610    return;
611
612}
613
614
615
Note: See TracBrowser for help on using the repository browser.