SHRINK.CPP

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 2.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 - 2007 OpenFX Development Team
00004 -- */
00005 
00006 /*  SHRINK.CPP  */
00007 
00008 #include "vtk.hh"
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <math.h>
00012 #include <windows.h>
00013 
00014 #define UNIT         32768L
00015 #define DESELECTED       0
00016 #define SELECTED         1
00017 #define INEDITOR         6
00018 
00019 #ifdef __cplusplus
00020 extern "C" {
00021 #endif
00022 
00023 #include "struct.h"
00024 #include "dstruct.h"
00025 
00026 #ifdef __cplusplus
00027 }
00028 #endif
00029 
00030 #include "shrink.h"
00031 
00032 static long *VidList=NULL,VidMax=0;
00033 
00034 class SfxReader : public vtkPolySource
00035 {
00036 public:
00037   SfxReader();
00038   ~SfxReader();
00039   char *GetClassName() {return "SfxReader";};
00040   int GetNumberOfVertices(void);
00041   int GetNumberOfEdge(void);
00042   int GetNumberOfFaces(void);
00043 
00044 protected:
00045   void Execute();
00046   int  Nv,Nf,Ne;
00047 };
00048 
00049 class SfxBaseWriter : public vtkObject
00050 {
00051 public:
00052   SfxBaseWriter();
00053   virtual void Write();
00054   void Update();
00055 
00056 protected:
00057   vtkDataSet *Input;
00058   virtual void WriteData() = 0;
00059 };
00060 
00061 class SfxWriter : public SfxBaseWriter
00062 {
00063 public:
00064   SfxWriter();
00065   ~SfxWriter();
00066   char *GetClassName() {return "SfxWriter";};
00067   void SetInput(vtkPolyData *input);
00068   void SetInput(vtkPolyData &input) {this->SetInput(&input);};
00069   vtkPolyData *GetInput() {return (vtkPolyData *)this->Input;};
00070   int GetNumberOfVertices(void);
00071   int GetNumberOfEdge(void);
00072   int GetNumberOfFaces(void);
00073 
00074 protected:
00075   int Nv,Ne,Nf;
00076   void WriteData();
00077   void InsertInVertexList(long , long , long );
00078   void BuildEdges(long );
00079   void EraseOldStructure(long );
00080   void OffsetStructure(long , long , long , long );
00081 };
00082 
00083 static HWND      hParent;
00084 static HINSTANCE hThisInstance;
00085 
00086 static BOOL CALLBACK SfxDlgProc(HWND hwnd, UINT msg,
00087                                 WPARAM wparam, LPARAM lparam);
00088 
00089 #if __WATCOMC__
00090 int APIENTRY LibMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00091 #else
00092 BOOL WINAPI DllMain(HANDLE hDLL, DWORD dwReason, LPVOID lpReserved){
00093 #endif
00094   switch (dwReason) {
00095     case DLL_PROCESS_ATTACH: {
00096       hThisInstance=(HINSTANCE)hDLL;
00097       if(hDLL == NULL)MessageBeep(MB_OK);
00098       break;
00099     }
00100     case DLL_PROCESS_DETACH:
00101       break;
00102   }
00103   return TRUE;
00104 }
00105 
00106 
00107 extern "C" BOOL _Xmodeler
00108  (HWND parent_window,HWND info_window,X__STRUCTURE *lpevi){
00109  HCURSOR hSave;
00110  static struct bp {
00111   float f;
00112  } d;
00113  lpEVI=lpevi;
00114  hParent=parent_window;
00115  d.f=0.8;
00116  if(DialogBoxParam(hThisInstance,MAKEINTRESOURCE(DLG_VTK),hParent,
00117              (DLGPROC)SfxDlgProc,(LPARAM)&d) == FALSE)return FALSE;
00118  hSave=SetCursor(LoadCursor(NULL,IDC_WAIT));
00119  {
00120   SfxReader sfxinput;
00121   SfxWriter sfxoutput;
00122   vtkShrinkPolyData shrink;
00123 
00124   shrink.SetInput(sfxinput.GetOutput());
00125   shrink.SetShrinkFactor(d.f);
00126 
00127   sfxoutput.SetInput(shrink.GetOutput());
00128   sfxoutput.Write();
00129  }
00130  SetCursor(hSave);
00131  return TRUE;
00132 }
00133 
00134 static BOOL CALLBACK SfxDlgProc(HWND hwnd, UINT msg,
00135                           WPARAM wparam, LPARAM lparam){
00136  BOOL err;
00137  double f;
00138  int i;
00139  static struct bp {
00140    float f;
00141  } *dp;
00142  char str[32];
00143  switch( msg ) {
00144    case WM_INITDIALOG:
00145      dp=(struct bp *)lparam;
00146      sprintf(str,"%.1lf",dp->f);
00147      SetDlgItemText(hwnd,DLG_SHRINKFACTOR,(LPCTSTR)str);
00148      CentreDlgOnS(hwnd);
00149      return (TRUE);
00150    case WM_COMMAND:
00151       switch(LOWORD(wparam)){
00152         case IDCANCEL:
00153           EndDialog(hwnd,FALSE);
00154           return(TRUE);
00155         case IDOK:
00156           if(GetDlgItemText(hwnd,DLG_SHRINKFACTOR,str,10) == 0){
00157             EndDialog(hwnd,FALSE); return TRUE;
00158           }
00159           dp->f=atof(str);
00160           EndDialog(hwnd,TRUE);
00161           return(TRUE);
00162         default:
00163           break;
00164       }
00165       break;
00166     default: break;
00167  }
00168  return(FALSE);
00169 }
00170 
00171 SfxReader::SfxReader()
00172 {
00173  Nv=Ne=Nf=0;
00174 }
00175 
00176 SfxReader::~SfxReader()
00177 {
00178 }
00179 
00180 int SfxReader::GetNumberOfVertices(void){return (int)this->Nv;}
00181 int SfxReader::GetNumberOfEdge(void){return (int)this->Ne;}
00182 int SfxReader::GetNumberOfFaces(void){return (int)this->Nf;}
00183 
00184 void SfxReader::Execute()
00185 {
00186   float x[3];
00187   int i,pts[3],count;
00188   vtkFloatPoints *newPoints;
00189   vtkCellArray *newTris;
00190   vtkPolyData *output = this->GetOutput();
00191   if(NvertSelect == 0)return;
00192   newPoints = new vtkFloatPoints(NvertSelect);
00193   for(count=0,i=0;i<Nvert;i++){
00194     if((MainVp+i)->status == SELECTED){
00195       x[0] = (float)((MainVp+i)->xyz[0])/((float)UNIT);
00196       x[1] = (float)((MainVp+i)->xyz[1])/((float)UNIT);
00197       x[2] = (float)((MainVp+i)->xyz[2])/((float)UNIT);
00198       (MainVp+i)->id=count++;
00199       newPoints->InsertNextPoint(x);
00200       this->Nv++;
00201     }
00202   }
00203   face *fp;
00204   for(i=0,count=0;i<Nface;i++){
00205     fp=(MainFp+i);
00206     if((MainVp+fp->V[0])->status == SELECTED &&
00207        (MainVp+fp->V[1])->status == SELECTED &&
00208        (MainVp+fp->V[2])->status == SELECTED)count++;
00209   }
00210   newTris = new vtkCellArray;
00211   newTris->Allocate(count);
00212   VidList=(long *)X__Malloc(count*sizeof(long));
00213   VidMax=count;
00214   for(i=0;i<Nface;i++){
00215     fp=(MainFp+i);
00216     if((MainVp+fp->V[0])->status == SELECTED &&
00217        (MainVp+fp->V[1])->status == SELECTED &&
00218        (MainVp+fp->V[2])->status == SELECTED){
00219       pts[0] = (int)((MainVp+fp->V[0])->id);
00220       pts[1] = (int)((MainVp+fp->V[1])->id);
00221       pts[2] = (int)((MainVp+fp->V[2])->id);
00222       newTris->InsertNextCell(3,pts);
00223       if(VidList != NULL)VidList[this->Nf]=i;
00224       this->Nf++;
00225     }
00226   }
00227   output->SetPoints(newPoints);
00228   newPoints->Delete();
00229   output->SetPolys(newTris);
00230   newTris->Delete();
00231   output->Squeeze();
00232 //MessageBox(NULL,"SfxReader Finished",NULL,MB_OK);
00233 }
00234 
00235 SfxBaseWriter::SfxBaseWriter()
00236 {
00237   this->Input = NULL;
00238 }
00239 
00240 void SfxBaseWriter::Write()
00241 {
00242   if ( !this->Input ) {
00243     return;
00244   }
00245   this->Input->Update();
00246   this->WriteData();
00247   this->Input->ReleaseData();
00248 }
00249 
00250 void SfxBaseWriter::Update()
00251 {
00252   this->Write();
00253 }
00254 
00255 SfxWriter::SfxWriter()
00256 {
00257  Nv=0; Ne=0; Nf=0;
00258 }
00259 
00260 SfxWriter::~SfxWriter()
00261 {
00262 }
00263 
00264 int SfxWriter::GetNumberOfVertices(void){return (int)this->Nv;}
00265 int SfxWriter::GetNumberOfEdge(void){return (int)this->Ne;}
00266 int SfxWriter::GetNumberOfFaces(void){return (int)this->Nf;}
00267 
00268 void SfxWriter::SetInput(vtkPolyData *input)
00269 {
00270   if ( this->Input != input )
00271     {
00272     this->Input = (vtkDataSet *) input;
00273     this->Modified();
00274     }
00275 }
00276 
00277 void SfxWriter::WriteData()
00278 {
00279   vtkPoints *pts;
00280   vtkCellArray *polys;
00281   vtkPolyData *input=(vtkPolyData *)this->Input;
00282 
00283   if ( (pts = input->GetPoints()) == NULL ||
00284   (polys = input->GetPolys()) == NULL ){
00285     return;
00286   }
00287   vertex *Vp;
00288   face   *Fp;
00289   float p[3];
00290   int i,npts,npolys;
00291   long BaseVert;
00292   BaseVert=Nvert;
00293   npts=input->GetNumberOfPoints();
00294   npolys=input->GetNumberOfPolys();
00295   this->Nv=npts;
00296   this->Nf=npolys;
00297   if(npts > 0){
00298     if(!UpdateVertexHeap(Nvert+npts))return;
00299     for(i=0;i<npts;i++){
00300       pts->GetPoint(i,p);
00301       CreateVertex();
00302       Vp=(MainVp+Nvert-1);
00303       Vp->id=0;
00304       Vp->sp=NULL;
00305       Vp->xyz[0]=(long)(p[0]*(float)UNIT);
00306       Vp->xyz[1]=(long)(p[1]*(float)UNIT);
00307       Vp->xyz[2]=(long)(p[2]*(float)UNIT);
00308     }
00309   }
00310   if(npolys > 0){
00311     int *indx;
00312     if(!UpdateFaceHeap(Nface+npolys))return;
00313     for (i=0,polys->InitTraversal(); polys->GetNextCell(npts,indx); i++){
00314       CreateFace(BaseVert+(long)indx[0],
00315                  BaseVert+(long)indx[1],
00316                  BaseVert+(long)indx[2]);
00317       this->InsertInVertexList(BaseVert,indx[0],indx[1]);
00318       this->InsertInVertexList(BaseVert,indx[1],indx[2]);
00319       this->InsertInVertexList(BaseVert,indx[2],indx[0]);
00320       if(VidList != NULL){
00321         if(i < VidMax){
00322           CopyFaceProp((MainFp+VidList[i]),(MainFp+Nface-1));
00323         }
00324       }
00325     }
00326     this->BuildEdges(BaseVert);
00327   }
00328   this->EraseOldStructure(BaseVert);
00329 }
00330 
00331 // Use the vertex's skeleton pointer and integer identifer as temporary
00332 // storage for build edge adjacency lists since they are not uses on new
00333 // vertices.
00334 //
00335 void SfxWriter::InsertInVertexList(long base, long Vj, long Vi){
00336  vertex *vj,*vi;
00337  long *list,i,j;
00338  vj=(MainVp+base+Vj);
00339  vi=(MainVp+base+Vi);
00340  list = (long *)vi->sp;
00341  if(vi->id > 0)for(i=0;i<vi->id;i++)if(list[i] == Vj)return;
00342  list = (long *)vj->sp;
00343  if(vj->id > 0)for(j=0;j<vj->id;j++)if(list[j] == Vi)return;
00344  if(vi->id == 0){
00345    if((list = (long *)X__Malloc((vi->id + 1)*sizeof(long))) == NULL){
00346      return;
00347    }
00348    vi->sp=(struct Designer_SKELETON_tag *)list;
00349  }
00350  else{
00351    list = (long *)vi->sp;
00352    if((list = (long *)X__Realloc(list,(vi->id + 1)*sizeof(long))) == NULL){
00353      return;
00354    }
00355    vi->sp=(struct Designer_SKELETON_tag *)list;
00356  }
00357  list[vi->id] = base+Vj;
00358  (vi->id)++;
00359 }
00360 
00361 void SfxWriter::BuildEdges(long base){
00362  long n,j,i,*list;
00363  vertex *vp;
00364  vp=(MainVp+base);
00365  for(j=base;j<Nvert;j++){
00366    if(vp->id != 0 && vp->sp != NULL){
00367      if(!UpdateEdgeHeap(Nedge+vp->id))return;
00368      list = (long *)vp->sp;
00369      for(i=0;i<vp->id;i++)CreateEdge(j,list[i]);
00370      X__Free(vp->sp); vp->sp=NULL; vp->id=0;
00371    }
00372    vp++;
00373  }
00374 }
00375 
00376 void SfxWriter::EraseOldStructure(long base){
00377  long c;
00378  vertex *vp;
00379  vp=(MainVp+base); for(c=base;c<Nvert;c++){
00380    if(vp->status == SELECTED){
00381      vp->status=INEDITOR; NvertSelect--; NvertDeselect++;
00382    }
00383    vp++;
00384  }
00385  EraseVertex(1);
00386  vp=MainVp; for(c=0;c<Nvert;c++){
00387    if(vp->status == INEDITOR){
00388      vp->status=SELECTED; NvertSelect++; NvertDeselect--;
00389    }
00390    vp++;
00391  }
00392 }
00393 
00394 void SfxWriter::OffsetStructure(long base, long dx, long dy, long dz){
00395  long c;
00396  vertex *vp;
00397  vp=(MainVp+base); for(c=base;c<Nvert;c++){
00398    vp->xyz[0] += dx;
00399    vp->xyz[1] += dy;
00400    vp->xyz[2] += dz;
00401    vp++;
00402  }
00403 }

Generated on Sun Apr 27 14:20:11 2014 for OpenFX by  doxygen 1.5.6