halfedge 结构

来源:互联网 发布:购买备案好的域名 编辑:程序博客网 时间:2024/04/28 02:09

本学期的高级计算机图像学作业的一部分, 实现halfedge结构。 我用obj结构和halfedge互相转化来完成这样一件事。


halfedge.h:

#ifndef HALFEDGE_VECTOR_H_#define HALFEDGE_VECTOR_H_#include <iostream>using namespace std;#include <vector>typedef struct  {float x;float y;float z;int edge_index;// whether a new pointbool isnew;// the neighbour of a pointvector<int> beighbour;} halfegde_point;typedef struct  {int one_edge_index;} halfedge_face;typedef struct  {int original_point_index;int face_index;int twin_edge_index;int next_edge_index;int previous_edge_index;// whether splitedbool issplited;// splited point's indexint split_point_index;} halfegde;//默认逆时针bool twins(vector<halfegde> e, halfegde a, halfegde b);// index begin from 0;// transform obj to halfedgevoid obj2halfedge(float (*point)[3], int point_count, int (*face)[3], int face_count, vector<halfegde_point> &halfegde_p, vector<halfedge_face> &halfegde_f, vector<halfegde> &e);// transform halfedge to objvoid halfedge2obj(vector<halfegde_point> halfegde_p, vector<halfedge_face> halfegde_f, vector<halfegde> e, float (*point)[3], int (*face)[3]);// calculate points neighbourvoid update_neighbour(vector<halfegde_point> &halfegde_p, vector<halfedge_face> &halfegde_f, vector<halfegde> &e);#endif

halfedeg.cpp:

#include "halfedge_vector.h"//默认逆时针bool twins(vector<halfegde> e, halfegde a, halfegde b){int abegin = a.original_point_index;int aend = e[a.next_edge_index].original_point_index;int bbegin = b.original_point_index;int bend  = e[b.next_edge_index].original_point_index;return((abegin == bend && aend == bbegin));}// index begin from 0;// face have 3 egdes;// couterclock// transform obj to halfedgevoid obj2halfedge(float (*point)[3], int point_count, int (*face)[3], int face_count, vector<halfegde_point> &halfegde_p, vector<halfedge_face> &halfegde_f, vector<halfegde> &e){// numint pcount = point_count;int ecount = 3*face_count;int fcount = face_count;halfegde_p.reserve(pcount);halfegde_f.reserve(face_count);e.reserve(ecount);// initialfor (int i = 0; i< point_count; i++){halfegde_point tmp;tmp.x = tmp.y = tmp.z = tmp.edge_index = -1;tmp.isnew = false;tmp.beighbour.clear();halfegde_p.push_back(tmp);}for(int i = 0; i< face_count; i++){halfedge_face tmp;tmp.one_edge_index = -1;halfegde_f.push_back(tmp);}for (int i = 0; i< 3*face_count; i++){halfegde tmp;tmp.face_index = tmp.next_edge_index = tmp.previous_edge_index = tmp.original_point_index = tmp.twin_edge_index = tmp.split_point_index = -1;tmp.issplited = false;e.push_back(tmp);}// write all pointsfor (int i = 0; i< point_count; i++){halfegde_p[i].x = point[i][0];halfegde_p[i].y = point[i][1];halfegde_p[i].z = point[i][2];halfegde_p[i].edge_index = -1;halfegde_p[i].isnew = false;}// write all edgefor (int i = 0; i< face_count; i++){for (int j = 0; j< 3; j++){e[3*i+j].original_point_index = face[i][j];halfegde_p[face[i][j]].edge_index = 3*i+j;e[3*i+j].face_index = i;halfegde_f[i].one_edge_index = 3*i+j;int nex = (j+1)%3;int prev = (j-1+3)%3;e[3*i+j].next_edge_index = 3*i+nex;e[3*i+j].previous_edge_index = 3*i+prev;e[3*i+j].issplited = false;e[3*i+j].split_point_index = -1;}}// twins edgefor (int i = 0; i< ecount; i++){for (int j = i+1; j< ecount; j++){int abegin = e[i].original_point_index;int aend = e[e[i].next_edge_index].original_point_index;int bbegin = e[j].original_point_index;int bend  = e[e[j].next_edge_index].original_point_index;if (abegin == bend&&aend == bbegin){e[i].twin_edge_index = j;e[j].twin_edge_index = i;break;}/*if (twins(e, e[i], e[j])&& i != j){e[i].twin_edge_index = j;break;}*/}}update_neighbour(halfegde_p, halfegde_f, e);}// transform halfedge to objvoid halfedge2obj(vector<halfegde_point> halfegde_p, vector<halfedge_face> halfegde_f, vector<halfegde> e, float (*point)[3], int (*face)[3]){// edge should be 3 times as faceint point_count = halfegde_p.size();int face_count = halfegde_f.size();// initialfor (int i = 0; i< point_count; i++){point[i][0] = point[i][1] = point[i][2] = -1;}for (int i = 0; i< face_count; i++){face[i][0] = face[i][1] = face[i][2] = -1;}// pointfor (int i = 0; i< point_count; i++){point[i][0] = halfegde_p[i].x;point[i][1] = halfegde_p[i].y;point[i][2] = halfegde_p[i].z;}//for (int i = 0; i< face_count; i++){int e1 = halfegde_f[i].one_edge_index;int p1 = e[e1].original_point_index;int e2 = e[e1].next_edge_index;int p2 = e[e2].original_point_index;int e3 = e[e2].next_edge_index;int p3 = e[e3].original_point_index;face[i][0] = p1;face[i][1] = p2;face[i][2] = p3;}}// 对于一个密闭曲面// 所有点最少有三个邻居// 并且所有点都会闭合void update_neighbour(vector<halfegde_point> &halfegde_p, vector<halfedge_face> &halfegde_f, vector<halfegde> &e){for (int i = 0; i< halfegde_p.size(); i++){vector<int> calculaterd;halfegde_point tmpp = halfegde_p[i];int eindex = tmpp.edge_index;int pindex = e[e[eindex].next_edge_index].original_point_index;calculaterd.push_back(pindex);int original_pindex = pindex;while(e[eindex].twin_edge_index != -1){eindex = e[eindex].twin_edge_index;eindex = e[eindex].next_edge_index;pindex = e[e[eindex].next_edge_index].original_point_index;if (original_pindex != pindex){calculaterd.push_back(pindex);}else{halfegde_p[i].beighbour = calculaterd;break;}}}}

over。

0 0