Kstars

SpatialEdge.cpp
1//# Filename: SpatialEdge.cpp
2//#
3//# The SpatialEdge class is defined here.
4//#
5//# Author: Peter Z. Kunszt based on A. Szalay's code
6//#
7//# Date: October 15, 1998
8//#
9//# SPDX-FileCopyrightText: 2000 Peter Z. Kunszt Alex S. Szalay, Aniruddha R. Thakar
10//# The Johns Hopkins University
11//#
12//# Modification History:
13//#
14//# Oct 18, 2001 : Dennis C. Dinge -- Replaced ValVec with std::vector
15//#
16
17#include "SpatialEdge.h"
18
19// ===========================================================================
20//
21// Macro definitions for readability
22//
23// ===========================================================================
24#define IV(x) tree_.nodes_[index].v_[(x)]
25#define IW(x) tree_.nodes_[index].w_[(x)]
26#define LAYER tree_.layers_[layerindex_]
27
28// ===========================================================================
29//
30// Member functions for class SpatialEdge
31//
32// ===========================================================================
33
34/////////////CONSTRUCTOR//////////////////////////////////
35//
36SpatialEdge::SpatialEdge(SpatialIndex &tree, size_t layerindex) : tree_(tree), layerindex_(layerindex)
37{
38 edges_ = new Edge[LAYER.nEdge_ + 1];
39 lTab_ = new Edge *[LAYER.nVert_ * 6];
40
41 // initialize lookup table, we depend on that nullptr
42 for (size_t i = 0; i < LAYER.nVert_ * 6; i++)
43 lTab_[i] = nullptr;
44
45 // first vertex index for the vertices to be generated
46 index_ = LAYER.nVert_;
47}
48
49/////////////DESTRUCTOR///////////////////////////////////
50//
51SpatialEdge::~SpatialEdge()
52{
53 delete[] edges_;
54 delete[] lTab_;
55}
56
57/////////////MAKEMIDPOINTS////////////////////////////////
58// makeMidPoints: interface to this class. Set midpoints of every
59// node in this layer.
60void SpatialEdge::makeMidPoints()
61{
62 size_t c = 0;
63 size_t index;
64
65 // build up the new edges
66
67 index = (size_t)LAYER.firstIndex_;
68 for (size_t i = 0; i < LAYER.nNode_; i++, index++)
69 {
70 c = newEdge(c, index, 0);
71 c = newEdge(c, index, 1);
72 c = newEdge(c, index, 2);
73 }
74}
75
76/////////////NEWEDGE//////////////////////////////////////
77// newEdge: determines whether the edge em is already in the list. k
78// is the label of the edge within the node Returns index of next
79// edge, if not found, or returns same if it is already there. Also
80// registers the midpoint in the node.
81
82size_t SpatialEdge::newEdge(size_t emindex, size_t index, int k)
83{
84 Edge *en, *em;
85 size_t swap;
86
87 em = &edges_[emindex];
88
89 switch (k)
90 {
91 case 0:
92 em->start_ = IV(1);
93 em->end_ = IV(2);
94 break;
95 case 1:
96 em->start_ = IV(0);
97 em->end_ = IV(2);
98 break;
99 case 2:
100 em->start_ = IV(0);
101 em->end_ = IV(1);
102 break;
103 }
104
105 // sort the vertices by increasing index
106
107 if (em->start_ > em->end_)
108 {
109 swap = em->start_;
110 em->start_ = em->end_;
111 em->end_ = swap;
112 }
113
114 // check all previous edges for a match, return pointer if
115 // already present, log the midpoint with the new face as well
116
117 if ((en = edgeMatch(em)) != nullptr)
118 {
119 IW(k) = en->mid_;
120 return emindex;
121 }
122
123 // this is a new edge, immediately process the midpoint,
124 // and save it with the nodes and the edge as well
125
126 insertLookup(em);
127 IW(k) = getMidPoint(em);
128 em->mid_ = IW(k);
129 return ++emindex;
130}
131
132/////////////INSERTLOOKUP/////////////////////////////////
133// insertLookup: insert the edge em into the lookup table.
134// indexed by em->start_.
135// Every vertex has at most 6 edges, so only
136// that much lookup needs to be done.
137void SpatialEdge::insertLookup(Edge *em)
138{
139 int j = 6 * em->start_;
140 int i;
141
142 // do not loop beyond 6
143
144 for (i = 0; i < 6; i++, j++)
145 {
146 if (lTab_[j] == nullptr)
147 {
148 lTab_[j] = em;
149 return;
150 }
151 }
152}
153
154/////////////EDGEMATCH////////////////////////////////////
155// edgeMatch: fast lookup using the first index em->start_.
156// return pointer to edge if matches, null if not.
157SpatialEdge::Edge *SpatialEdge::edgeMatch(Edge *em)
158{
159 int i = 6 * em->start_;
160
161 while (lTab_[i] != nullptr)
162 {
163 if (em->end_ == lTab_[i]->end_)
164 return lTab_[i];
165
166 i++;
167 }
168 return nullptr;
169}
170
171/////////////GETMIDPOINT//////////////////////////////////
172// getMidPoint: compute the midpoint of the edge using vector
173// algebra and return its index in the vertex list
174size_t SpatialEdge::getMidPoint(Edge *em)
175{
176 tree_.vertices_[index_] = tree_.vertices_[em->start_] + tree_.vertices_[em->end_];
177 tree_.vertices_[index_].normalize();
178 return index_++;
179}
SpatialIndex is a quad tree of spherical triangles.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.