Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kicad-source-mirror
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Elphel
kicad-source-mirror
Commits
cce6e270
Commit
cce6e270
authored
May 14, 2014
by
Maciej Suminski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Various fixes to geometry & math library.
parent
d27ea789
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
299 additions
and
141 deletions
+299
-141
shape_collisions.cpp
common/geometry/shape_collisions.cpp
+186
-85
shape_line_chain.cpp
common/geometry/shape_line_chain.cpp
+49
-41
seg.h
include/geometry/seg.h
+8
-8
shape_circle.h
include/geometry/shape_circle.h
+11
-0
shape_line_chain.h
include/geometry/shape_line_chain.h
+20
-0
shape_rect.h
include/geometry/shape_rect.h
+18
-6
math_util.h
include/math/math_util.h
+5
-0
vector2d.h
include/math/vector2d.h
+2
-1
No files found.
common/geometry/shape_collisions.cpp
View file @
cce6e270
This diff is collapsed.
Click to expand it.
common/geometry/shape_line_chain.cpp
View file @
cce6e270
...
...
@@ -135,6 +135,9 @@ int SHAPE_LINE_CHAIN::Distance( const VECTOR2I& aP ) const
{
int
d
=
INT_MAX
;
if
(
IsClosed
()
&&
PointInside
(
aP
)
)
return
0
;
for
(
int
s
=
0
;
s
<
SegmentCount
();
s
++
)
d
=
std
::
min
(
d
,
CSegment
(
s
).
Distance
(
aP
)
);
...
...
@@ -179,7 +182,7 @@ int SHAPE_LINE_CHAIN::Split( const VECTOR2I& aP )
int
SHAPE_LINE_CHAIN
::
Find
(
const
VECTOR2I
&
aP
)
const
{
for
(
int
s
=
0
;
s
<
PointCount
();
s
++
)
for
(
int
s
=
0
;
s
<
PointCount
();
s
++
)
if
(
CPoint
(
s
)
==
aP
)
return
s
;
...
...
@@ -187,6 +190,16 @@ int SHAPE_LINE_CHAIN::Find( const VECTOR2I& aP ) const
}
int
SHAPE_LINE_CHAIN
::
FindSegment
(
const
VECTOR2I
&
aP
)
const
{
for
(
int
s
=
0
;
s
<
SegmentCount
();
s
++
)
if
(
CSegment
(
s
).
Distance
(
aP
)
<=
1
)
return
s
;
return
-
1
;
}
const
SHAPE_LINE_CHAIN
SHAPE_LINE_CHAIN
::
Slice
(
int
aStartIndex
,
int
aEndIndex
)
const
{
SHAPE_LINE_CHAIN
rv
;
...
...
@@ -261,6 +274,9 @@ int SHAPE_LINE_CHAIN::Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS&
if
(
a
.
Collinear
(
b
)
)
{
is
.
our
=
a
;
is
.
their
=
b
;
if
(
a
.
Contains
(
b
.
A
)
)
{
is
.
p
=
b
.
A
;
aIp
.
push_back
(
is
);
}
if
(
a
.
Contains
(
b
.
B
)
)
{
is
.
p
=
b
.
B
;
aIp
.
push_back
(
is
);
}
if
(
b
.
Contains
(
a
.
A
)
)
{
is
.
p
=
a
.
A
;
aIp
.
push_back
(
is
);
}
...
...
@@ -282,44 +298,6 @@ int SHAPE_LINE_CHAIN::Intersect( const SHAPE_LINE_CHAIN& aChain, INTERSECTIONS&
}
return
aIp
.
size
();
for
(
int
s1
=
0
;
s1
<
SegmentCount
();
s1
++
)
{
for
(
int
s2
=
0
;
s2
<
aChain
.
SegmentCount
();
s2
++
)
{
const
SEG
&
a
=
CSegment
(
s1
);
const
SEG
&
b
=
aChain
.
CSegment
(
s2
);
OPT_VECTOR2I
p
=
a
.
Intersect
(
b
);
INTERSECTION
is
;
if
(
p
)
{
is
.
p
=
*
p
;
is
.
our
=
a
;
is
.
their
=
b
;
aIp
.
push_back
(
is
);
}
else
if
(
a
.
Collinear
(
b
)
)
{
if
(
a
.
A
!=
b
.
A
&&
a
.
A
!=
b
.
B
&&
b
.
Contains
(
a
.
A
)
)
{
is
.
p
=
a
.
A
;
is
.
our
=
a
;
is
.
their
=
b
;
aIp
.
push_back
(
is
);
}
else
if
(
a
.
B
!=
b
.
A
&&
a
.
B
!=
b
.
B
&&
b
.
Contains
(
a
.
B
)
)
{
is
.
p
=
a
.
B
;
is
.
our
=
a
;
is
.
their
=
b
;
aIp
.
push_back
(
is
);
}
}
}
}
return
aIp
.
size
();
}
...
...
@@ -372,10 +350,13 @@ bool SHAPE_LINE_CHAIN::PointInside( const VECTOR2I& aP ) const
bool
SHAPE_LINE_CHAIN
::
PointOnEdge
(
const
VECTOR2I
&
aP
)
const
{
if
(
SegmentCount
()
<
1
)
if
(
!
PointCount
()
)
return
false
;
else
if
(
PointCount
()
==
1
)
return
m_points
[
0
]
==
aP
;
for
(
int
i
=
1
;
i
<
SegmentCount
();
i
++
)
for
(
int
i
=
0
;
i
<
SegmentCount
();
i
++
)
{
const
SEG
s
=
CSegment
(
i
);
...
...
@@ -534,3 +515,30 @@ const std::string SHAPE_LINE_CHAIN::Format() const
return
ss
.
str
();
}
bool
SHAPE_LINE_CHAIN
::
CompareGeometry
(
const
SHAPE_LINE_CHAIN
&
aOther
)
const
{
SHAPE_LINE_CHAIN
a
(
*
this
),
b
(
aOther
);
a
.
Simplify
();
b
.
Simplify
();
if
(
a
.
m_points
.
size
()
!=
b
.
m_points
.
size
())
return
false
;
for
(
int
i
=
0
;
i
<
a
.
PointCount
();
i
++
)
if
(
a
.
CPoint
(
i
)
!=
b
.
CPoint
(
i
))
return
false
;
return
true
;
}
bool
SHAPE_LINE_CHAIN
::
Intersects
(
const
SHAPE_LINE_CHAIN
&
aChain
)
const
{
INTERSECTIONS
dummy
;
return
Intersect
(
aChain
,
dummy
)
!=
0
;
}
SHAPE
*
SHAPE_LINE_CHAIN
::
Clone
()
const
{
return
new
SHAPE_LINE_CHAIN
(
*
this
);
}
include/geometry/seg.h
View file @
cce6e270
...
...
@@ -244,14 +244,14 @@ public:
*/
bool
Collinear
(
const
SEG
&
aSeg
)
const
{
ecoord
qa
=
A
.
y
-
B
.
y
;
ecoord
qb
=
B
.
x
-
A
.
x
;
ecoord
qc
=
-
qa
*
A
.
x
-
qb
*
A
.
y
;
ecoord
d1
=
std
::
abs
(
aSeg
.
A
.
x
*
qa
+
aSeg
.
A
.
y
*
qb
+
qc
)
;
ecoord
d2
=
std
::
abs
(
aSeg
.
B
.
x
*
qa
+
aSeg
.
B
.
y
*
qb
+
qc
)
;
return
(
d1
<=
1
&&
d2
<=
1
);
ecoord
qa
1
=
A
.
y
-
B
.
y
;
ecoord
qb
1
=
B
.
x
-
A
.
x
;
ecoord
qc
1
=
-
qa1
*
A
.
x
-
qb1
*
A
.
y
;
ecoord
qa2
=
aSeg
.
A
.
y
-
aSeg
.
B
.
y
;
ecoord
qb2
=
aSeg
.
B
.
x
-
aSeg
.
A
.
x
;
ecoord
qc2
=
-
qa2
*
aSeg
.
A
.
x
-
qb2
*
aSeg
.
A
.
y
;
return
(
qa1
==
qa2
)
&&
(
qb1
==
qb2
)
&&
(
qc1
==
qc2
);
}
/**
...
...
include/geometry/shape_circle.h
View file @
cce6e270
...
...
@@ -38,9 +38,20 @@ public:
SHAPE
(
SH_CIRCLE
),
m_radius
(
aRadius
),
m_center
(
aCenter
)
{}
SHAPE_CIRCLE
(
const
SHAPE_CIRCLE
&
aOther
)
:
SHAPE
(
SH_CIRCLE
),
m_radius
(
aOther
.
m_radius
),
m_center
(
aOther
.
m_center
)
{};
~
SHAPE_CIRCLE
()
{}
SHAPE
*
Clone
()
const
{
return
new
SHAPE_CIRCLE
(
*
this
);
}
const
BOX2I
BBox
(
int
aClearance
=
0
)
const
{
const
VECTOR2I
rc
(
m_radius
+
aClearance
,
m_radius
+
aClearance
);
...
...
include/geometry/shape_line_chain.h
View file @
cce6e270
...
...
@@ -116,6 +116,8 @@ public:
~
SHAPE_LINE_CHAIN
()
{}
SHAPE
*
Clone
()
const
;
/**
* Function Clear()
* Removes all points from the line chain.
...
...
@@ -366,6 +368,11 @@ public:
}
}
void
Insert
(
int
aVertex
,
const
VECTOR2I
&
aP
)
{
m_points
.
insert
(
m_points
.
begin
()
+
aVertex
,
aP
);
}
/**
* Function Replace()
*
...
...
@@ -417,6 +424,15 @@ public:
*/
int
Find
(
const
VECTOR2I
&
aP
)
const
;
/**
* Function FindSegment()
*
* Searches for segment containing point aP.
* @param aP the point to be looked for
* @return index of the correspoinding segment in the line chain or negative when not found.
*/
int
FindSegment
(
const
VECTOR2I
&
aP
)
const
;
/**
* Function Slice()
*
...
...
@@ -441,6 +457,8 @@ public:
VECTOR2I
m_origin
;
};
bool
Intersects
(
const
SHAPE_LINE_CHAIN
&
aChain
)
const
;
/**
* Function Intersect()
*
...
...
@@ -533,6 +551,8 @@ public:
return
false
;
}
bool
CompareGeometry
(
const
SHAPE_LINE_CHAIN
&
aOther
)
const
;
private
:
/// array of vertices
std
::
vector
<
VECTOR2I
>
m_points
;
...
...
include/geometry/shape_rect.h
View file @
cce6e270
...
...
@@ -53,10 +53,22 @@ public:
* Constructor
* Creates a rectangle defined by top-left corner aP0, width aW and height aH.
*/
SHAPE_RECT
(
const
VECTOR2I
&
aP0
,
int
aW
,
int
aH
)
:
SHAPE_RECT
(
const
VECTOR2I
&
aP0
,
int
aW
,
int
aH
)
:
SHAPE
(
SH_RECT
),
m_p0
(
aP0
),
m_w
(
aW
),
m_h
(
aH
)
{}
SHAPE_RECT
(
const
SHAPE_RECT
&
aOther
)
:
SHAPE
(
SH_RECT
),
m_p0
(
aOther
.
m_p0
),
m_w
(
aOther
.
m_w
),
m_h
(
aOther
.
m_h
)
{};
SHAPE
*
Clone
()
const
{
return
new
SHAPE_RECT
(
*
this
);
}
/// @copydoc SHAPE::BBox()
const
BOX2I
BBox
(
int
aClearance
=
0
)
const
{
...
...
@@ -90,11 +102,11 @@ public:
if
(
BBox
(
0
).
Contains
(
aSeg
.
A
)
||
BBox
(
0
).
Contains
(
aSeg
.
B
)
)
return
true
;
VECTOR2I
vts
[]
=
{
VECTOR2I
(
m_p0
.
x
,
m_p0
.
y
),
VECTOR2I
(
m_p0
.
x
,
m_p0
.
y
+
m_h
),
VECTOR2I
(
m_p0
.
x
+
m_w
,
m_p0
.
y
+
m_h
),
VECTOR2I
(
m_p0
.
x
+
m_w
,
m_p0
.
y
),
VECTOR2I
(
m_p0
.
x
,
m_p0
.
y
)
};
VECTOR2I
vts
[]
=
{
VECTOR2I
(
m_p0
.
x
,
m_p0
.
y
),
VECTOR2I
(
m_p0
.
x
,
m_p0
.
y
+
m_h
),
VECTOR2I
(
m_p0
.
x
+
m_w
,
m_p0
.
y
+
m_h
),
VECTOR2I
(
m_p0
.
x
+
m_w
,
m_p0
.
y
),
VECTOR2I
(
m_p0
.
x
,
m_p0
.
y
)
};
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
...
...
include/math/math_util.h
View file @
cce6e270
...
...
@@ -40,6 +40,11 @@ T rescale( T aNumerator, T aValue, T aDenominator )
return
aNumerator
*
aValue
/
aDenominator
;
}
template
<
typename
T
>
int
sign
(
T
val
)
{
return
(
T
(
0
)
<
val
)
-
(
val
<
T
(
0
)
);
}
// explicit specializations for integer types, taking care of overflow.
template
<>
...
...
include/math/vector2d.h
View file @
cce6e270
...
...
@@ -28,6 +28,7 @@
#ifndef VECTOR2D_H_
#define VECTOR2D_H_
#include <cmath>
#include <climits>
#include <iostream>
#include <sstream>
...
...
@@ -376,7 +377,7 @@ VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
return
VECTOR2
<
T
>
(
(
x
<
0
?
-
1
:
1
)
*
sqrt
(
rescale
(
l_sq_new
,
(
extended_type
)
x
*
x
,
l_sq_current
)
),
(
y
<
0
?
-
1
:
1
)
*
sqrt
(
rescale
(
l_sq_new
,
(
extended_type
)
y
*
y
,
l_sq_current
)
)
);
(
y
<
0
?
-
1
:
1
)
*
sqrt
(
rescale
(
l_sq_new
,
(
extended_type
)
y
*
y
,
l_sq_current
)
)
)
*
sign
(
aNewLength
)
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment