Visual Computing Library  devel
Loading...
Searching...
No Matches
segment.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_SEGMENT_H
24#define VCL_SPACE_CORE_SEGMENT_H
25
26#include "point.h"
27
28namespace vcl {
29
39template<PointConcept PointT>
41{
42 PointT mPoint0;
43 PointT mPoint1;
44
45public:
50 using PointType = PointT;
51
55 using ScalarType = PointT::ScalarType;
56
60 static const uint DIM = PointT::DIM;
61
67
74 Segment(const PointT& p0, const PointT& p1) : mPoint0(p0), mPoint1(p1) {}
75
81 PointT& p0() { return mPoint0; }
82
88 const PointT& p0() const { return mPoint0; }
89
95 PointT& p1() { return mPoint1; }
96
102 const PointT& p1() const { return mPoint1; }
103
104 PointT midPoint() const { return (mPoint0 + mPoint1) / 2.0; }
105
106 PointT direction() const { return mPoint1 - mPoint0; }
107
108 PointT normalizedDirection() const
109 {
110 return (mPoint1 - mPoint0).normalize();
111 }
112
113 ScalarType length() const { (mPoint0 - mPoint1).norm(); }
114
115 ScalarType squaredLength() const { (mPoint0 - mPoint1).squaredNorm(); }
116
117 void flip() { std::swap(mPoint0, mPoint1); }
118
119 bool operator==(const Segment<PointT>& s) const = default;
120
121 bool operator!=(const Segment<PointT>& s) const = default;
122
123 Segment<PointT> operator+(const Segment<PointT>& s) const
124 {
125 return Segment<PointT>(mPoint0 + s.mPoint0, mPoint1 + s.mPoint1);
126 }
127
128 Segment<PointT> operator-(const Segment<PointT>& s) const
129 {
130 return Segment<PointT>(mPoint0 - s.mPoint0, mPoint1 - s.mPoint1);
131 }
132
133 Segment<PointT> operator*(const ScalarType& s) const
134 {
135 return Segment<PointT>(mPoint0 * s, mPoint1 * s);
136 }
137
138 Segment<PointT> operator/(const ScalarType& s) const
139 {
140 return Segment<PointT>(mPoint0 / s, mPoint1 / s);
141 }
142
143 Segment<PointT>& operator+=(const Segment<PointT>& s) const
144 {
145 mPoint0 += s.mPoint0;
146 mPoint1 += s.mPoint1;
147 return *this;
148 }
149
150 Segment<PointT>& operator-=(const Segment<PointT>& s) const
151 {
152 mPoint0 -= s.mPoint0;
153 mPoint1 -= s.mPoint1;
154 return *this;
155 }
156
157 Segment<PointT>& operator*=(const ScalarType& s) const
158 {
159 mPoint0 *= s;
160 mPoint1 *= s;
161 return *this;
162 }
163
164 Segment<PointT>& operator/=(const ScalarType& s) const
165 {
166 mPoint0 /= s;
167 mPoint1 /= s;
168 return *this;
169 }
170};
171
172/* Specialization Aliases */
173
174template<typename S>
175using Segment2 = Segment<Point2<S>>;
176
177using Segment2i = Segment<Point2i>;
178using Segment2f = Segment<Point2f>;
179using Segment2d = Segment<Point2d>;
180
181template<typename S>
182using Segment3 = Segment<Point3<S>>;
183
184using Segment3i = Segment<Point3i>;
185using Segment3f = Segment<Point3f>;
186using Segment3d = Segment<Point3d>;
187
188/* Concepts */
189
200template<typename T>
201concept SegmentConcept = std::derived_from< // same type or derived type
202 std::remove_cvref_t<T>,
203 Segment<typename RemoveRef<T>::PointType>>;
204
215template<typename T>
216concept Segment2Concept = SegmentConcept<T> && RemoveRef<T>::DIM == 2;
217
228template<typename T>
229concept Segment3Concept = SegmentConcept<T> && RemoveRef<T>::DIM == 3;
230
231} // namespace vcl
232
233#endif // VCL_SPACE_CORE_SEGMENT_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
PointT::ScalarType ScalarType
The scalar type of the endpoint points.
Definition segment.h:55
static const uint DIM
The dimensionality of the segment.
Definition segment.h:60
PointT & p0()
Returns the first endpoint of the segment.
Definition segment.h:81
PointT & p1()
Returns the second endpoint of the segment.
Definition segment.h:95
Segment()
Default constructor. Creates a segment with endpoints at the origin.
Definition segment.h:66
const PointT & p1() const
Returns the second endpoint of the segment.
Definition segment.h:102
Segment(const PointT &p0, const PointT &p1)
Creates a segment with the given endpoints.
Definition segment.h:74
const PointT & p0() const
Returns the first endpoint of the segment.
Definition segment.h:88
PointT PointType
The type of point used to represent the endpoint points of the segment.
Definition segment.h:50
A concept representing a 2D Segment.
Definition segment.h:216
A concept representing a 3D Segment.
Definition segment.h:229
A concept representing a Segment.
Definition segment.h:201