Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
grafika elso hf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Börcsök Barnabás
grafika elso hf
Commits
15a15ae2
Commit
15a15ae2
authored
5 years ago
by
bobarna
Browse files
Options
Downloads
Patches
Plain Diff
first iteration (not working) ear cutting
parent
4b245990
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
sirius.cpp
+101
-24
101 additions, 24 deletions
sirius.cpp
with
101 additions
and
24 deletions
sirius.cpp
+
101
−
24
View file @
15a15ae2
...
...
@@ -121,12 +121,9 @@ public:
};
class
BaseCircle
:
public
Drawable
{
BaseCircle
()
=
default
;
public:
static
BaseCircle
generate
()
{
BaseCircle
baseCircle
;
std
::
vector
<
vec2
>
&
vertices
=
baseCircle
.
vertices
;
BaseCircle
()
{
// calculate all the vertices of the base circle:
float
step
=
360.0
/
BASE_CIRCLE_SEGMENTS
;
float
currAngle
=
0
;
...
...
@@ -144,11 +141,10 @@ public:
currAngle
+=
step
;
vertices
.
emplace_back
(
cos
(
degToRad
(
currAngle
)),
sin
(
degToRad
(
currAngle
)));
}
return
baseCircle
;
}
void
draw
()
override
{
init
();
glBindVertexArray
(
vao
);
// Draw call
glDrawArrays
(
GL_TRIANGLES
,
0
,
vertices
.
size
());
}
...
...
@@ -198,20 +194,95 @@ public:
printf
(
"%f %f
\n
"
,
cx
,
cy
);
c
=
vec2
(
cx
,
cy
);
r
=
sqrtf
((
c
.
x
-
p1
.
x
)
*
(
c
.
x
-
p1
.
x
)
+
(
c
.
y
-
p1
.
y
)
*
(
c
.
y
-
p1
.
y
));
}
};
inline
float
lengthv2
(
vec2
v
)
{
return
sqrt
(
v
.
x
*
v
.
x
+
v
.
y
*
v
.
y
);
}
inline
float
crossv2
(
vec2
a
,
vec2
b
)
{
return
a
.
x
*
b
.
y
-
a
.
y
*
b
.
x
;
}
inline
bool
intersectEachOther
(
vec2
p1
,
vec2
p2
,
vec2
q1
,
vec2
q2
)
{
return
lengthv2
(
crossv2
(
q1
-
q2
,
p1
-
q1
))
*
lengthv2
(
crossv2
(
q1
-
q2
,
p2
-
q1
))
<
0
&&
lengthv2
(
crossv2
(
q1
-
q2
,
p1
-
q1
))
*
lengthv2
(
crossv2
(
q1
-
q2
,
p2
-
q1
))
<
0
;
}
class
Triangulated
:
public
Drawable
{
std
::
vector
<
vec2
>
remainingVertices
;
public:
explicit
Triangulated
(
std
::
vector
<
vec2
>
points
)
{
remainingVertices
=
points
;
vertices
.
reserve
(
remainingVertices
.
size
()
*
3
);
triangulate
();
};
// init();
void
triangulate
()
{
while
(
!
remainingVertices
.
empty
())
{
//finding an ear
auto
currVertex
=
remainingVertices
.
begin
();
//check if current vertex is an ear
vec2
currPotDiag1
=
*
getNextVertex
(
currVertex
);
vec2
currPotDiag2
=
*
getPreviousVertex
(
currVertex
);
//check if current potential diagonal intersects any edges
// skipping edges that share a vertex with it
for
(
auto
currEdgeStart
=
getPreviousVertex
(
getPreviousVertex
(
currVertex
));
currEdgeStart
!=
getNextVertex
(
getNextVertex
(
currVertex
));
currEdgeStart
=
getPreviousVertex
(
currEdgeStart
))
if
(
intersectEachOther
(
vec2
(
currEdgeStart
->
x
,
currEdgeStart
->
y
),
vec2
(
getNextVertex
(
currEdgeStart
)
->
x
,
getNextVertex
(
currEdgeStart
)
->
y
),
currPotDiag1
,
currPotDiag2
))
break
;
//check if current potential diagonal is fully outside the polygon
vec2
infinity
(
250
,
250
);
vec2
middle
((
currPotDiag1
+
currPotDiag2
)
/
2
);
int
numberOfIntersections
=
0
;
for
(
auto
currEdgeStart
=
remainingVertices
.
begin
();
currEdgeStart
!=
remainingVertices
.
begin
();
currEdgeStart
=
getPreviousVertex
(
currEdgeStart
))
if
(
intersectEachOther
(
currEdgeStart
->
x
,
*
getNextVertex
(
currEdgeStart
),
infinity
,
middle
))
numberOfIntersections
++
;
if
(
numberOfIntersections
%
2
==
0
)
break
;
// the potential diagonal is fully outside, it's not an ear.
//it's an ear!
vertices
.
emplace_back
(
*
currVertex
);
vertices
.
emplace_back
(
currPotDiag1
);
vertices
.
emplace_back
(
currPotDiag2
);
remainingVertices
.
erase
(
currVertex
);
}
}
std
::
vector
<
vec2
>::
iterator
getPreviousVertex
(
std
::
vector
<
vec2
>::
iterator
currVertex
)
{
if
(
currVertex
==
remainingVertices
.
begin
())
return
remainingVertices
.
end
();
else
return
--
currVertex
;
}
// void draw() override {
// glBindVertexArray(vao); // Draw call
// setColor(vec3(1, 0, 0));
// glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
// }
std
::
vector
<
vec2
>::
iterator
getNextVertex
(
std
::
vector
<
vec2
>::
iterator
currVertex
)
{
if
(
currVertex
==
remainingVertices
.
end
())
return
remainingVertices
.
begin
();
else
return
++
currVertex
;
};
void
draw
()
override
{
init
();
glBindVertexArray
(
vao
);
// Draw call
glDrawArrays
(
GL_TRIANGLES
,
0
,
vertices
.
size
());
}
};
class
SiriusTriangle
:
public
Drawable
{
std
::
vector
<
Point
>
points
;
std
::
vector
<
Triangulated
>
triangulated
;
public:
int
n
=
0
;
...
...
@@ -224,8 +295,11 @@ public:
if
(
n
>
3
)
printf
(
"Can't add 4th point to a triangle!
\n
"
);
printf
(
"%d
\n
"
,
n
);
if
(
n
==
2
)
generateLineSegment
(
points
[
0
],
points
[
1
]);
if
(
n
==
3
)
generateLineSegment
(
points
[
2
],
points
[
0
]);
if
(
n
==
3
)
generateLineSegment
(
points
[
1
],
points
[
2
]);
if
(
n
==
3
)
{
generateLineSegment
(
points
[
2
],
points
[
0
]);
generateLineSegment
(
points
[
1
],
points
[
2
]);
triangulated
.
emplace_back
(
Triangulated
(
vertices
));
}
}
...
...
@@ -270,16 +344,22 @@ public:
}
void
draw
()
override
{
for
(
auto
&
point
:
points
)
point
.
draw
();
//filling out the triangulated area
if
(
n
==
3
)
triangulated
[
0
].
draw
();
//Time to draw the edges
if
(
n
==
3
)
{
init
();
glBindVertexArray
(
vao
);
// Draw call
setColor
(
vec3
(
1
,
0
,
0
));
setColor
(
vec3
(
0.6
,
0
.2
,
0
.6
));
glDrawArrays
(
GL_LINE_LOOP
,
0
,
vertices
.
size
());
}
for
(
auto
&
point
:
points
)
point
.
draw
();
}
};
...
...
@@ -305,7 +385,7 @@ public:
}
};
BaseCircle
baseCircle
=
BaseCircle
::
generate
();
BaseCircle
baseCircle
=
BaseCircle
();
SiriusTriangleManager
siriusTriangleManager
=
SiriusTriangleManager
();
// Initialization, create an OpenGL context
...
...
@@ -314,7 +394,7 @@ void onInitialization() {
baseCircle
.
init
();
siriusTriangleManager
.
addPoint
(
vec2
(
-
0.6
,
0.4
));
siriusTriangleManager
.
addPoint
(
vec2
(
-
0.8
,
0.2
));
siriusTriangleManager
.
addPoint
(
vec2
(
-
0.8
,
-
0.2
));
siriusTriangleManager
.
addPoint
(
vec2
(
-
0.2
,
-
0.6
));
// create program for the GPU
...
...
@@ -383,11 +463,6 @@ void onMouse(int button, int state, int pX,
break
;
}
// siriusTriangleManager.addPoint(vec2(-.8f, -.4f));
// siriusTriangleManager.addPoint(vec2(-.2f, -.6f));
// siriusTriangleManager.addPoint(vec2(-.4f, .4f));
// siriusTriangleManager.draw();
if
(
state
==
GLUT_DOWN
&&
button
==
GLUT_LEFT_BUTTON
)
{
siriusTriangleManager
.
addPoint
(
vec2
(
cX
,
cY
));
siriusTriangleManager
.
draw
();
...
...
@@ -399,3 +474,5 @@ void onMouse(int button, int state, int pX,
void
onIdle
()
{
long
time
=
glutGet
(
GLUT_ELAPSED_TIME
);
// elapsed time since the start of the program
}
#include
<utility>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment