-
Notifications
You must be signed in to change notification settings - Fork 9
/
geometry.cpp
172 lines (138 loc) · 3.53 KB
/
geometry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <string>
#include <algorithm>
#include "cnotcounter.h"
#include "geometry.h"
#include "gatenumbers.h"
#include "circuitmatrix.h"
int geometry::addIOPoint(convertcoordinate& ioc, bool isInit)
{
int ioIndex = addCoordinate(ioc);
io.push_back(ioIndex);
return ioIndex;
}
bool geometry::removeSegment(int idx1, int idx2)
{
if(idx1 == idx2)
{
//printf("zero distance...do not add\n");
return false;
}
std::pair<long, long> s1 = computePair(idx1, idx2);
std::string key = computeKey(s1);
//do not assume the segment exists
std::map<std::string, long>::iterator it = segMap.find(key);
if(it == segMap.end())
{
return false;
}
std::vector<std::pair<long, long> >::iterator it2 = find(segs.begin(), segs.end(), s1);
segs.erase(it2);
segMap.erase(it);
return true;
}
std::pair<long, long> geometry::computePair(int idx1, int idx2)
{
std::pair<long, long> s1;
//indicele mic e primul, al doilea e indicele mare
s1.first = idx1 < idx2 ? idx1 : idx2;
s1.second = idx1 > idx2 ? idx1 : idx2;
return s1;
}
std::string geometry::computeKey(std::pair<long, long>& p)
{
/*new linear search*/
std::string key;
char numstr[100]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%ld,%ld", p.first, p.second);
key = key + numstr;
return key;
}
bool geometry::addSegment(int idx1, int idx2)
{
return addSegment(idx1, idx2, DONOTADDEXISTINGSEGMENT);
}
bool geometry::addSegment(int idx1, int idx2, int operationTypeForExistingSegment)
{
if(idx1 == idx2)
{
//printf("zero distance...do not add\n");
return false;
}
std::pair<long, long> s1 = computePair(idx1, idx2);
/*new linear search*/
std::string key = computeKey(s1);
// char numstr[100]; // enough to hold all numbers up to 64-bits
// sprintf(numstr, "%d,%d", s1.first, s1.second);
// key = key + numstr;
std::map<std::string, long>::iterator it = segMap.find(key);
if(it != segMap.end())
{
if(operationTypeForExistingSegment == DONOTADDEXISTINGSEGMENT)
{
//do not nothing for now
}
if(operationTypeForExistingSegment == REMOVEEXISTINGSEGMENT)
{
segs.erase(segs.begin() + it->second);
segMap.erase(it);
}
return false;
}
segs.push_back(s1);
segMap[key] = segs.size() - 1;
return true;
}
int geometry::addCoordinate(convertcoordinate& coord)
{
/*new linear search*/
std::string key = coord.toString(',');
std::map<std::string, long>::iterator it = coordMap.find(key);
if(it != coordMap.end())
{
return it->second;
}
//compute bounding box
updateBoundingBox(coord);
coords.push_back(coord);
coordMap[key] = coords.size() - 1;
return coords.size() - 1;
}
void geometry::updateBoundingBox(convertcoordinate& coord)
{
//compute bounding box
for (int i = 0; i < 3; i++)
{
if (boundingbox[i] < coord[i])
boundingbox[i] = coord[i];
}
}
void geometry::reset()
{
coords.clear();
coordMap.clear();
segs.clear();
segMap.clear();
boundingbox.reset();
}
void geometry::appendGeometry(geometry& other, convertcoordinate& offset)
{
std::map<long, long> indexOfIndex;
for(size_t i = 0; i < other.coords.size(); i++)
{
convertcoordinate c2 = other.coords[i];
c2[CIRCUITWIDTH] += offset[CIRCUITWIDTH];
c2[CIRCUITDEPTH] += offset[CIRCUITDEPTH];
c2[CIRCUITHEIGHT] += offset[CIRCUITHEIGHT];
long cindex = addCoordinate(c2);
indexOfIndex[i] = cindex;
}
for(size_t i = 0; i < other.segs.size(); i++)
{
addSegment(indexOfIndex[other.segs[i].first], indexOfIndex[other.segs[i].second], DONOTADDEXISTINGSEGMENT);
}
for(size_t i = 0; i < other.io.size(); i++)
{
io.push_back(indexOfIndex[other.io[i]]);
}
}