Visual Computing Library
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
28#include <vclib/concepts/space/segment.h>
29
30namespace vcl {
31
41template<PointConcept PointT>
43{
44 PointT mPoint0;
45 PointT mPoint1;
46
47public:
52 using PointType = PointT;
53
57 using ScalarType = PointT::ScalarType;
58
62 static const uint DIM = PointT::DIM;
63
69
76 Segment(const PointT& p0, const PointT& p1) : mPoint0(p0), mPoint1(p1) {}
77
83 PointT& p0() { return mPoint0; }
84
90 const PointT& p0() const { return mPoint0; }
91
97 PointT& p1() { return mPoint1; }
98
104 const PointT& p1() const { return mPoint1; }
105
106 PointT midPoint() const { return (mPoint0 + mPoint1) / 2.0; }
107
108 PointT direction() const { return mPoint1 - mPoint0; }
109
110 PointT normalizedDirection() const
111 {
112 return (mPoint1 - mPoint0).normalize();
113 }
114
115 ScalarType length() const { (mPoint0 - mPoint1).norm(); }
116
117 ScalarType squaredLength() const { (mPoint0 - mPoint1).squaredNorm(); }
118
119 void flip() { std::swap(mPoint0, mPoint1); }
120
121 bool operator==(const Segment<PointT>& s) const = default;
122
123 bool operator!=(const Segment<PointT>& s) const = default;
124
125 Segment<PointT> operator+(const Segment<PointT>& s) const
126 {
127 return Segment<PointT>(mPoint0 + s.mPoint0, mPoint1 + s.mPoint1);
128 }
129
130 Segment<PointT> operator-(const Segment<PointT>& s) const
131 {
132 return Segment<PointT>(mPoint0 - s.mPoint0, mPoint1 - s.mPoint1);
133 }
134
135 Segment<PointT> operator*(const ScalarType& s) const
136 {
137 return Segment<PointT>(mPoint0 * s, mPoint1 * s);
138 }
139
140 Segment<PointT> operator/(const ScalarType& s) const
141 {
142 return Segment<PointT>(mPoint0 / s, mPoint1 / s);
143 }
144
145 Segment<PointT>& operator+=(const Segment<PointT>& s) const
146 {
147 mPoint0 += s.mPoint0;
148 mPoint1 += s.mPoint1;
149 return *this;
150 }
151
152 Segment<PointT>& operator-=(const Segment<PointT>& s) const
153 {
154 mPoint0 -= s.mPoint0;
155 mPoint1 -= s.mPoint1;
156 return *this;
157 }
158
159 Segment<PointT>& operator*=(const ScalarType& s) const
160 {
161 mPoint0 *= s;
162 mPoint1 *= s;
163 return *this;
164 }
165
166 Segment<PointT>& operator/=(const ScalarType& s) const
167 {
168 mPoint0 /= s;
169 mPoint1 /= s;
170 return *this;
171 }
172};
173
174/* Specialization Aliases */
175
176template<typename S>
177using Segment2 = Segment<Point2<S>>;
178
179using Segment2i = Segment<Point2i>;
180using Segment2f = Segment<Point2f>;
181using Segment2d = Segment<Point2d>;
182
183template<typename S>
184using Segment3 = Segment<Point3<S>>;
185
186using Segment3i = Segment<Point3i>;
187using Segment3f = Segment<Point3f>;
188using Segment3d = Segment<Point3d>;
189
190} // namespace vcl
191
192#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:43
PointT::ScalarType ScalarType
The scalar type of the endpoint points.
Definition segment.h:57
static const uint DIM
The dimensionality of the segment.
Definition segment.h:62
PointT & p0()
Returns the first endpoint of the segment.
Definition segment.h:83
PointT & p1()
Returns the second endpoint of the segment.
Definition segment.h:97
Segment()
Default constructor. Creates a segment with endpoints at the origin.
Definition segment.h:68
const PointT & p1() const
Returns the second endpoint of the segment.
Definition segment.h:104
Segment(const PointT &p0, const PointT &p1)
Creates a segment with the given endpoints.
Definition segment.h:76
const PointT & p0() const
Returns the first endpoint of the segment.
Definition segment.h:90
PointT PointType
The type of point used to represent the endpoint points of the segment.
Definition segment.h:52