Allink  v0.1
VarDataPos.cpp
00001 /***********************************************************************
00002 VarDataEl: Elaboration functions for the VarData class. This functions
00003 provides a simple manipulation of the data read by [Open]. The
00004 options are provided to elaborate different system's shapes.
00005 Copyright (C) 2008 by Giovanni Marelli <sabeiro@virgilio.it>
00006 
00007 
00008 This program is free software; you can redistribute it and/or modify
00009 it under the terms of the GNU General Public License as published by
00010 the Free Software Foundation; either version 2 of the License, or
00011 (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 ***********************************************************************/
00022 #include "../include/VarData.h"
00023 
00024 int VarData::Arrange(int **Triangle,int Vertex){
00025   BfDefChain();
00026   int *nChain = (int *)calloc(Vertex,sizeof(int));
00027   double *dChain= (double *)calloc(Vertex,sizeof(double));
00028   double MaxLength = 1.5*sqrt(QUAD(Gen->Edge[0])+QUAD(Gen->Edge[1]));
00029   for(int c=0;c<Gen->NChain;c++){
00030     for(int v=0;v<Vertex;v++)
00031       dChain[v] = MaxLength;
00032     int NVertex = 0;
00033     if(!CHAIN_IF_TYPE(Ch[c].Type,NChType))continue;
00034     for(int cc=0;cc<Gen->NChain;cc++){
00035       if(!CHAIN_IF_TYPE(Ch[cc].Type,NChType))continue;
00036       if(c == cc) continue;
00037       double Shift[3] = {0.,0.,0.};
00038       double PosDiff[3] = {0.,0.,0.};
00039       for(int d=0;d<3;d++){
00040    PosDiff[d] = Ch[cc].Pos[d] - Ch[c].Pos[d];
00041    Shift[d] = -floor((PosDiff[d])/Gen->Edge[d]+.5)*Gen->Edge[d];
00042       }
00043       double Dist = QUAD(PosDiff[CLat1]+Shift[CLat1]) + QUAD(PosDiff[CLat2]+Shift[CLat2]);// + QUAD(PosZ);
00044       //if(Dist > MaxLength) continue;
00045       //printf("%d %d %lf %lf\n",c,cc,Dist,dChain[0]);
00046       for(int v=0;v<Vertex;v++){
00047    if(dChain[v] > Dist){
00048      //printf("%d %d/%d %lf %lf\n",c,cc,v,Dist,dChain[v]);
00049      for(int vv=Vertex-1;vv>v;vv--){
00050        dChain[vv] = dChain[vv-1];
00051        nChain[vv] = nChain[vv-1];
00052      }
00053      dChain[v] = Dist;
00054      nChain[v] = cc;
00055      NVertex++;
00056      //for(int v=0;v<Vertex;v++)printf("%d %d\n",v,nChain[v]);
00057      break;
00058    }
00059       }
00060     }
00061     for(int v=0;v<Vertex;v++)
00062       Triangle[v][c] = nChain[v];
00063     Triangle[Vertex][c] = NVertex<Vertex ? NVertex : Vertex;
00064     //Triangle[Vertex-1][c] = c;
00065     //for(int v=0;v<Vertex+1;v++) printf("%d ",Triangle[v][c]); printf("\n");
00066   }
00067   free(nChain);
00068   free(dChain);
00069   return 1;
00070 }
00071 int VarData::OrderPos(){
00072   // for(int c=0;c<Gen->NChain;c++){
00073   //   Ch[c].nPos = CalcnPos(Ch[c].Pos);
00074   //   //    printf("%d %d\n",c,Ch[c].nPos);
00075   //   for(int cc=c-1;cc>=0;cc--){
00076   //     int nProl = cc==0 ? 0 : Ch[cc-1].nPos;
00077   //     //printf("%d<%d<%d | %d<=%d<%d\n",cc,c,cc-1,Ch[cc].nPos,Ch[c].nPos,nProl);
00078   //     if(Ch[c].nPos < Ch[cc].nPos && Ch[c].nPos >= nProl){
00079   //    //printf("--%d/%d - %d/%d \n",c,cc+1,Ch[c].nPos,nProl);
00080   //    for(int ccc=c;ccc>cc;ccc--) SwapChain(ccc,ccc-1);
00081   //    break;
00082   //     }
00083   //   }
00084   //     //for(int cc=0;cc<=c;cc++)printf("%d %d\n",cc,Ch[cc].nPos);
00085   // }
00086   // return 1;
00087 }
00088 int VarData::CalcnPos(double *Pos){
00089   double BlurDist[3]={Gen->Edge[0]/(double)SubDiv[0],Gen->Edge[1]/(double)SubDiv[1],Gen->Edge[2]/(double)SubDiv[2]};
00090   int v[3];
00091   for(int d=0;d<3;d++){
00092     double InvSubDiv = 1./(double)SubDiv[d];
00093     v[d] = (int)(Pos[d]/Gen->Edge[d]*SubDiv[d]);
00094     if(v[d] > SubDiv[d]){printf("Chain out of range %d>%d\n",v[d],SubDiv[d]); continue;}
00095     if(Pos[d] - v[d]*Gen->Edge[d]*InvSubDiv < .25*BlurDist[d])
00096       v[d] = 2*v[d] + 1;
00097     else if(Pos[d] - v[d]*Gen->Edge[d]*InvSubDiv > .75*BlurDist[d])
00098       v[d] = 2*v[d] + 3;
00099     else 
00100       v[d] = 2*v[d];
00101     //printf("%d\n",v[d]);
00102   }
00103   int nPos =  (v[0]*SubDiv[1] + v[1])*SubDiv[2]+v[2];
00104   return nPos;
00105 }
00106 int VarData::PosVectInt(double *Pos){
00107   int vx = (int)(Pos[CLat1]/Gen->Edge[CLat1]*NEdge);
00108   int vy = (int)(Pos[CLat2]/Gen->Edge[CLat2]*NEdge);
00109   int vz = (int)(Pos[CNorm]/Gen->Edge[CNorm]*NEdge);
00110   return (vx*NEdge + vy)*NEdge+vz;
00111 }