2019-08-14 21:38:37 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/renderer/Geometry.h>
|
|
|
|
|
#include <kiwano/renderer/Renderer.h>
|
2019-11-13 14:33:15 +08:00
|
|
|
#include <kiwano/core/win32/helper.h>
|
2019-08-14 21:38:37 +08:00
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Geometry
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
Geometry::Geometry()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 21:15:15 +08:00
|
|
|
bool Geometry::IsValid() const
|
|
|
|
|
{
|
|
|
|
|
return geo_ != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 15:29:32 +08:00
|
|
|
Rect Geometry::GetBoundingBox() const
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
2019-08-27 15:29:32 +08:00
|
|
|
Rect bounds;
|
|
|
|
|
if (geo_)
|
|
|
|
|
{
|
|
|
|
|
// no matter it failed or not
|
|
|
|
|
geo_->GetBounds(nullptr, DX::ConvertToRectF(&bounds));
|
|
|
|
|
}
|
|
|
|
|
return bounds;
|
|
|
|
|
}
|
2019-08-14 21:38:37 +08:00
|
|
|
|
2019-08-27 15:29:32 +08:00
|
|
|
Rect Geometry::GetBoundingBox(Matrix3x2 const& transform) const
|
|
|
|
|
{
|
|
|
|
|
Rect bounds;
|
|
|
|
|
if (geo_)
|
|
|
|
|
{
|
|
|
|
|
// no matter it failed or not
|
|
|
|
|
geo_->GetBounds(DX::ConvertToMatrix3x2F(transform), DX::ConvertToRectF(&bounds));
|
|
|
|
|
}
|
|
|
|
|
return bounds;
|
2019-08-14 21:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
float Geometry::GetLength() const
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
2019-09-29 22:23:13 +08:00
|
|
|
float length = 0.f;
|
2019-08-14 21:38:37 +08:00
|
|
|
if (geo_)
|
|
|
|
|
{
|
|
|
|
|
// no matter it failed or not
|
|
|
|
|
geo_->ComputeLength(D2D1::Matrix3x2F::Identity(), &length);
|
|
|
|
|
}
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
bool Geometry::ComputePointAtLength(float length, Point& point, Vec2& tangent) const
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
|
|
|
|
if (geo_)
|
|
|
|
|
{
|
|
|
|
|
HRESULT hr = geo_->ComputePointAtLength(
|
|
|
|
|
length,
|
|
|
|
|
D2D1::Matrix3x2F::Identity(),
|
|
|
|
|
DX::ConvertToPoint2F(&point),
|
|
|
|
|
DX::ConvertToPoint2F(&tangent)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 10:51:34 +08:00
|
|
|
void Geometry::Clear()
|
|
|
|
|
{
|
|
|
|
|
geo_.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
void Geometry::CombineWith(GeometrySink& sink, Geometry input, CombineMode mode, Matrix3x2 const& input_matrix) const
|
2019-08-14 23:36:29 +08:00
|
|
|
{
|
|
|
|
|
if (geo_ && input.geo_)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfFailed(
|
|
|
|
|
geo_->CombineWithGeometry(
|
|
|
|
|
input.geo_.get(),
|
|
|
|
|
D2D1_COMBINE_MODE(mode),
|
|
|
|
|
DX::ConvertToMatrix3x2F(input_matrix),
|
|
|
|
|
sink.GetGeometrySink().get()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
Geometry Geometry::CombineWith(Geometry input, CombineMode mode, Matrix3x2 const& input_matrix) const
|
2019-08-14 23:36:29 +08:00
|
|
|
{
|
2019-12-11 13:44:40 +08:00
|
|
|
GeometrySink sink;
|
|
|
|
|
sink.Open();
|
|
|
|
|
|
|
|
|
|
CombineWith(sink, input, mode, input_matrix);
|
|
|
|
|
|
|
|
|
|
sink.Close();
|
|
|
|
|
return sink.GetGeometry();
|
2019-08-14 23:36:29 +08:00
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
float Geometry::ComputeArea() const
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
|
|
|
|
if (!geo_)
|
|
|
|
|
return 0.f;
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
float area = 0.f;
|
2019-08-14 21:38:37 +08:00
|
|
|
// no matter it failed or not
|
|
|
|
|
geo_->ComputeArea(D2D1::Matrix3x2F::Identity(), &area);
|
|
|
|
|
return area;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 21:15:15 +08:00
|
|
|
bool Geometry::ContainsPoint(Point const& point, Matrix3x2 const& transform) const
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
|
|
|
|
if (!geo_)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
BOOL ret = 0;
|
|
|
|
|
// no matter it failed or not
|
|
|
|
|
geo_->FillContainsPoint(
|
|
|
|
|
DX::ConvertToPoint2F(point),
|
2019-08-20 21:15:15 +08:00
|
|
|
DX::ConvertToMatrix3x2F(transform),
|
2019-08-14 21:38:37 +08:00
|
|
|
&ret
|
|
|
|
|
);
|
|
|
|
|
return !!ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
Geometry Geometry::CreateLine(Point const& begin, Point const& end)
|
|
|
|
|
{
|
|
|
|
|
Geometry output;
|
2019-12-17 20:47:55 +08:00
|
|
|
Renderer::instance().CreateLineGeometry(output, begin, end);
|
2019-08-14 23:36:29 +08:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Geometry Geometry::CreateRect(Rect const& rect)
|
|
|
|
|
{
|
|
|
|
|
Geometry output;
|
2019-12-17 20:47:55 +08:00
|
|
|
Renderer::instance().CreateRectGeometry(output, rect);
|
2019-08-14 23:36:29 +08:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Geometry Geometry::CreateRoundedRect(Rect const& rect, Vec2 const& radius)
|
|
|
|
|
{
|
|
|
|
|
Geometry output;
|
2019-12-17 20:47:55 +08:00
|
|
|
Renderer::instance().CreateRoundedRectGeometry(output, rect, radius);
|
2019-08-14 23:36:29 +08:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
Geometry Geometry::CreateCircle(Point const& center, float radius)
|
2019-08-14 23:36:29 +08:00
|
|
|
{
|
|
|
|
|
Geometry output;
|
2019-12-17 20:47:55 +08:00
|
|
|
Renderer::instance().CreateEllipseGeometry(output, center, Vec2{ radius, radius });
|
2019-08-14 23:36:29 +08:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Geometry Geometry::CreateEllipse(Point const& center, Vec2 const& radius)
|
|
|
|
|
{
|
|
|
|
|
Geometry output;
|
2019-12-17 20:47:55 +08:00
|
|
|
Renderer::instance().CreateEllipseGeometry(output, center, radius);
|
2019-08-14 23:36:29 +08:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
//
|
|
|
|
|
// GeometrySink
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
GeometrySink::GeometrySink()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
GeometrySink::~GeometrySink()
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
2019-12-11 13:44:40 +08:00
|
|
|
Close();
|
|
|
|
|
}
|
2019-08-14 21:38:37 +08:00
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
GeometrySink& GeometrySink::BeginPath(Point const& begin_pos)
|
|
|
|
|
{
|
2019-08-14 21:38:37 +08:00
|
|
|
if (!sink_)
|
|
|
|
|
{
|
2019-12-11 13:44:40 +08:00
|
|
|
Open();
|
2019-08-14 21:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
sink_->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
|
2019-08-14 21:38:37 +08:00
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometrySink& GeometrySink::EndPath(bool closed)
|
|
|
|
|
{
|
|
|
|
|
if (sink_)
|
|
|
|
|
{
|
|
|
|
|
sink_->EndFigure(closed ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN);
|
|
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
Close();
|
2019-08-14 21:38:37 +08:00
|
|
|
}
|
|
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometrySink& GeometrySink::AddLine(Point const& point)
|
|
|
|
|
{
|
|
|
|
|
if (!sink_) BeginPath();
|
|
|
|
|
|
|
|
|
|
sink_->AddLine(DX::ConvertToPoint2F(point));
|
|
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometrySink& GeometrySink::AddLines(Vector<Point> const& points)
|
|
|
|
|
{
|
|
|
|
|
if (!sink_) BeginPath();
|
|
|
|
|
|
|
|
|
|
sink_->AddLines(
|
|
|
|
|
reinterpret_cast<const D2D_POINT_2F*>(&points[0]),
|
2019-10-12 11:26:41 +08:00
|
|
|
static_cast<uint32_t>(points.size())
|
2019-08-14 21:38:37 +08:00
|
|
|
);
|
|
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 11:26:41 +08:00
|
|
|
GeometrySink& kiwano::GeometrySink::AddLines(const Point* points, size_t count)
|
2019-08-26 10:22:58 +08:00
|
|
|
{
|
|
|
|
|
if (!sink_) BeginPath();
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
sink_->AddLines(reinterpret_cast<const D2D_POINT_2F*>(points), UINT32(count));
|
2019-08-26 10:22:58 +08:00
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
GeometrySink& GeometrySink::AddBezier(Point const& point1, Point const& point2, Point const& point3)
|
|
|
|
|
{
|
|
|
|
|
if (!sink_) BeginPath();
|
|
|
|
|
|
|
|
|
|
sink_->AddBezier(
|
|
|
|
|
D2D1::BezierSegment(
|
|
|
|
|
DX::ConvertToPoint2F(point1),
|
|
|
|
|
DX::ConvertToPoint2F(point2),
|
|
|
|
|
DX::ConvertToPoint2F(point3)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 22:23:13 +08:00
|
|
|
GeometrySink& GeometrySink::AddArc(Point const& point, Size const& radius, float rotation, bool clockwise, bool is_small)
|
2019-08-14 21:38:37 +08:00
|
|
|
{
|
|
|
|
|
if (!sink_) BeginPath();
|
|
|
|
|
|
|
|
|
|
sink_->AddArc(
|
|
|
|
|
D2D1::ArcSegment(
|
|
|
|
|
DX::ConvertToPoint2F(point),
|
|
|
|
|
DX::ConvertToSizeF(radius),
|
|
|
|
|
rotation,
|
|
|
|
|
clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE,
|
|
|
|
|
is_small ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
return (*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Geometry GeometrySink::GetGeometry()
|
|
|
|
|
{
|
|
|
|
|
if (sink_)
|
|
|
|
|
{
|
|
|
|
|
EndPath();
|
|
|
|
|
}
|
2019-12-27 10:51:34 +08:00
|
|
|
|
|
|
|
|
Geometry geo;
|
|
|
|
|
geo.SetGeometry(path_geo_);
|
|
|
|
|
return geo;
|
2019-08-14 21:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
void GeometrySink::Init()
|
|
|
|
|
{
|
|
|
|
|
if (!path_geo_)
|
|
|
|
|
{
|
2019-12-27 10:51:34 +08:00
|
|
|
Renderer::instance().CreateGeometrySink(*this);
|
2019-08-14 23:36:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
void GeometrySink::Open()
|
2019-08-14 23:36:29 +08:00
|
|
|
{
|
2019-12-11 13:44:40 +08:00
|
|
|
Init();
|
|
|
|
|
|
2019-08-14 23:36:29 +08:00
|
|
|
if (!sink_)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfFailed(path_geo_->Open(&sink_));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 13:44:40 +08:00
|
|
|
void GeometrySink::Close()
|
2019-08-14 23:36:29 +08:00
|
|
|
{
|
|
|
|
|
if (sink_)
|
|
|
|
|
{
|
|
|
|
|
ThrowIfFailed(sink_->Close());
|
|
|
|
|
|
|
|
|
|
sink_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:38:37 +08:00
|
|
|
}
|