Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue #72] Add spline support to vector graphic libraries #73

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.

The precise terms and conditions for copying, distribution and
The precise terms and conditions for copying, double_distribution and
modification follow.

TERMS AND CONDITIONS
Expand Down Expand Up @@ -93,7 +93,7 @@ on the Program.
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
double_distribution (with or without modification), making available to the
public, and in some countries other activities as well.

To "convey" a work means any kind of propagation that enables other
Expand Down Expand Up @@ -235,7 +235,7 @@ terms of section 4, provided that you also meet all of these conditions:
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
in or on a volume of a storage or double_distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
Expand All @@ -250,12 +250,12 @@ machine-readable Corresponding Source under the terms of this License,
in one of these ways:

a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
(including a physical double_distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.

b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
(including a physical double_distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
Expand Down
2 changes: 1 addition & 1 deletion libs/File2DProcessingTools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ file(GLOB File2DProcessingTools_HEADERS ./include/*.hpp)
if (SCRATCH_STATIC_LIB_LINKING)
add_library(File2DProcessingTools STATIC ${File2DProcessingTools_SOURCES} ${File2DProcessingTools_HEADERS})
else (SCRATCH_STATIC_LIB_LINKING)
add_library(File2DProcessingTools SHARED ${File2DProcessingTools_SOURCES} ${File2DProcessingTools_HEADERS})
add_library(File2DProcessingTools SHARED ${File2DProcessingTools_SOURCES} ${File2DProcessingTools_HEADERS} include/CPath.hpp source/CPath.cpp)
endif (SCRATCH_STATIC_LIB_LINKING)

target_include_directories(File2DProcessingTools PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand Down
103 changes: 103 additions & 0 deletions libs/File2DProcessingTools/include/CPath.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include <vector>
#include "CVectorGraphicsData.hpp"

namespace File2DProcessingTools {

class CPath {
private:
unsigned int _width;
CVectorGraphicsData::CColor_T _color;
bool _smooth;
bool _isCycled;
std::vector<Geometry2D::CPoint2D> CPoints2D;

public:
/**
* Default constructor. Width 1px, default color BLACK, smooth=false
*/
CPath() noexcept;

/**
* Create and setup CPath object
* @param width_px Width in px
* @param color Color
* @param smooth Smooth flag
*/
CPath(unsigned int width_px, CVectorGraphicsData::CColor_T &color, bool smooth) noexcept;

/**
* Set new color
* @param color New color
*/
void setColor(CVectorGraphicsData::CColor_T &color) noexcept;

/**
* Get color of the CPath object
* @return Color of the CPath object
*/
CVectorGraphicsData::CColor_T getColor() const noexcept;

/**
* Set width in px
* @param width_px Width in px
*/
void setWidth(unsigned int width_px) noexcept;

/**
* Get width of the CPath object
* @return Width of the CPath object in px
*/
unsigned int getWidth() const noexcept;

/**
* Set smoothing flag
* @param flag True to enable smoothing, false to disable
*/
void setSmooth(bool flag) noexcept;

/**
* Get smoothing flag
* @return Smoothing flag value
*/
bool isSmooth() const noexcept;

/**
* Add next CPath point. To add split point, pass {NaN, NaN}
* @param point Next CPath point
*/
void appendPoint(const Geometry2D::CPoint2D &point) noexcept;

/**
* Concatenate CPath object
* @param cPath CPath object to contatenate with
*/
void appendPath(CPath &cPath) noexcept;

/**
* Get stored points
* @return Stored points
*/
const std::vector<Geometry2D::CPoint2D> &getPoints() const noexcept;

/**
* Split current path to paths (split criterion - {NaN, NaN} point)
* @return
*/
std::vector<CPath> toSeparatedPaths();

/**
* Update internal is-current-path-cycled flag (ending == beginning)
*/
void updateCycled() noexcept;

/**
* Get is-current-path-cycled flag (ending == beginning)
* @return True if current path is cycled
*/
bool isCycled() const noexcept;

};

}
20 changes: 13 additions & 7 deletions libs/File2DProcessingTools/include/CVectorGraphicsData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@
#include "CLineSegment2D.hpp"

namespace File2DProcessingTools {


/**
* Vector graphics storage
* @author Nikita Novgorodtsev (github <https://github.com/paNoNi>)
*/
class CVectorGraphicsData {

private:

std::vector<Geometry2D::CLineSegment2D> _data;
std::vector<Geometry2D::CLineSegment2D> _line_segments;
std::vector<unsigned int> _line_segments_widths;

double min_x = std::numeric_limits<double>::max();
double min_y = std::numeric_limits<double>::max();
double max_x = -std::numeric_limits<double>::max();
double max_y = -std::numeric_limits<double>::max();
std::vector<unsigned int> _widths;

public:

Expand All @@ -48,6 +46,14 @@ namespace File2DProcessingTools {

CColor_T() = default;

CColor_T(const CColor_T &color) = default;

CColor_T(CColor_T &&color) = default;

CColor_T& operator=(const CColor_T &color) = default;
CColor_T& operator=(CColor_T &&color) = default;


/**
* Set color from RGB palette
* @param red - red level
Expand Down Expand Up @@ -129,8 +135,8 @@ namespace File2DProcessingTools {
*/
template<typename InputIterator>
void addLineSegments(InputIterator begin, InputIterator end, unsigned int width_pixels = 1) noexcept {
_data.insert(_data.end(), begin, end);
_widths.emplace_back(width_pixels);
_line_segments.insert(_line_segments.end(), begin, end);
_line_segments_widths.emplace_back(width_pixels);
_colors.emplace_back(_current_color);
}

Expand Down
90 changes: 90 additions & 0 deletions libs/File2DProcessingTools/source/CPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

#include <CPath.hpp>

namespace File2DProcessingTools {

void CPath::setColor(CVectorGraphicsData::CColor_T &color) noexcept {
_color = color;
}

CPath::CPath() noexcept {
_width = 1;;
_smooth = false;
_isCycled = false;
}

CPath::CPath(unsigned int width_px, CVectorGraphicsData::CColor_T &color, bool smooth) noexcept {
_width = width_px;
_color = color;
_smooth = smooth;
_isCycled = false;
}

CVectorGraphicsData::CColor_T CPath::getColor() const noexcept {
return _color;
}

void CPath::setWidth(unsigned int width_px) noexcept {
_width = width_px;
}

unsigned int CPath::getWidth() const noexcept {
return _width;
}

void CPath::setSmooth(bool flag) noexcept {
_smooth = flag;
}

bool CPath::isSmooth() const noexcept {
return _smooth;
}

void CPath::appendPoint(const Geometry2D::CPoint2D &point) noexcept {
CPoints2D.emplace_back(point);
}

std::vector<CPath> CPath::toSeparatedPaths() {
std::vector<CPath> paths;
bool createNextPath = true;
for (auto &point : CPoints2D) {
if (!std::isnormal(point.getX()) || !std::isnormal(point.getY())) {
createNextPath = true;
} else {
if (createNextPath) {
paths.emplace_back(this->_width, this->_color, this->_smooth);
createNextPath = false;
}
paths.back().appendPoint(point);
}
}
return paths;
}

const std::vector<Geometry2D::CPoint2D> &CPath::getPoints() const noexcept {
return CPoints2D;
}

void CPath::updateCycled() noexcept {

_isCycled = CPoints2D.size() > 1 && CPoints2D[0].getX() == CPoints2D[CPoints2D.size() - 1].getX() &&
CPoints2D[0].getY() == CPoints2D[CPoints2D.size() - 1].getY();
for (auto &point : CPoints2D) {
if (!std::isnormal(point.getX()) || !std::isnormal(point.getY())) {
_isCycled = false;
break;
}
}
}

bool CPath::isCycled() const noexcept {
return _isCycled;
}

void CPath::appendPath(CPath &cPath) noexcept {
std::vector<Geometry2D::CPoint2D> points2D = cPath.getPoints();
for(auto & i : points2D) {
this->appendPoint(i);
}
}
}
8 changes: 4 additions & 4 deletions libs/File2DProcessingTools/source/CVectorGraphicData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace File2DProcessingTools {
void
CVectorGraphicsData::addLineSegments(Geometry2D::CLineSegment2D line_segment, unsigned int width_pixels) noexcept {
checkRange(line_segment);
_data.emplace_back(line_segment);
_widths.emplace_back(width_pixels);
_line_segments.emplace_back(line_segment);
_line_segments_widths.emplace_back(width_pixels);
_colors.emplace_back(_current_color);
}


const std::vector<Geometry2D::CLineSegment2D> &CVectorGraphicsData::getLineSegments() const noexcept {
return _data;
return _line_segments;
}


Expand Down Expand Up @@ -65,7 +65,7 @@ namespace File2DProcessingTools {
}

const std::vector<unsigned int> &CVectorGraphicsData::getLineSegmentsWidths() const noexcept {
return _widths;
return _line_segments_widths;
}

const std::vector<CVectorGraphicsData::CColor_T> &CVectorGraphicsData::getLineSegmentsColors() const noexcept {
Expand Down
2 changes: 1 addition & 1 deletion libs/File2DProcessingTools/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
file(GLOB File2DProcessingTools_TEST_SOURCES ./*.cpp)

add_executable(File2DProcessingToolsUnitTests ${File2DProcessingTools_TEST_SOURCES})
add_executable(File2DProcessingToolsUnitTests ${File2DProcessingTools_TEST_SOURCES} CPathUnitTests.cpp)

target_include_directories(File2DProcessingToolsUnitTests PRIVATE ${File2DProcessingTools_SOURCE_DIR}/include)
target_include_directories(File2DProcessingToolsUnitTests PRIVATE ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
Expand Down
Loading