1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
#include <iostream>
#include <cmath>
#include <vector>
#include <iomanip>
struct Radian {
double rad;
};
// Structure to represent degrees, minutes, and seconds of an angle
struct Angle {
int degrees;
int minutes;
double seconds;
double radians;
//copy constructor
Angle(const Angle& other) {
degrees = other.degrees;
minutes = other.minutes;
seconds = other.seconds;
radians = other.radians;
}
// Constructor to initialize the angle and calculate radians
Angle(int _degrees, int _minutes, double _seconds)
: degrees(_degrees), minutes(_minutes), seconds(_seconds) {
// Calculate radians using degreesToRadians function
radians = degreesToRadians(degrees, minutes, seconds);
}
// Constructor using decimal degrees
Angle(double DecimalDeg) {
decimalToDMS(DecimalDeg, degrees, minutes, seconds);
radians = degreesToRadians(degrees, minutes, seconds);
}
// Constructor using radians
Angle (Radian radiansIn) {
//std::cout << "M_PI is "<< M_PI <<"\n";
radians = radiansIn.rad;
double DecimalDeg = radians * 180.0 / M_PI;
decimalToDMS(DecimalDeg, degrees, minutes, seconds);
}
// Function to convert degrees, minutes, and seconds to radians
static double degreesToRadians(int degrees, int minutes, double seconds) {
double totalDegrees = degrees + (double)minutes / 60.0 + seconds / 3600.0;
return totalDegrees * M_PI / 180.0;
}
// Function to convert decimal degrees to unsigned int degrees, unsigned int minutes, and double seconds
void decimalToDMS(double decimalDegrees, int& degrees, int& minutes, double& seconds) {
// Extract degrees
degrees = static_cast<unsigned int>(decimalDegrees);
// Calculate remaining decimal part (minutes and seconds)
double remainder = std::abs(decimalDegrees - static_cast<double>(degrees)) * 60.0;
// Extract minutes
minutes = static_cast<unsigned int>(remainder);
// Calculate remaining decimal part (seconds)
seconds = (remainder - static_cast<double>(minutes)) * 60.0;
}
};
// Structure to represent a 2D point
struct Point {
double x, y;
Point() = default;
Point(double _x, double _y) : x(_x), y(_y) {}
Point(const Point& other) {
x = other.x;
y = other.y;
}
};
class Resection {
private:
std::vector<Point> TrianglePoints;
std::vector<Angle> ObservedAngles;
std::vector<Angle> TriangleAngles;
std::vector<double> Kvalues;
double TotalKvalues = 0.0;
Point OccupiedPt;
public:
Resection(const std::vector<Point>& TrianglePointsIn,
const std::vector<Angle>& ObservedAnglesIn)
{
TrianglePoints = TrianglePointsIn;
ObservedAngles = ObservedAnglesIn;
calculateTriangleAngles();
calcKvalues();
TotalK();
}
private:
// Function to calculate the angles of the triangle in radians
void calculateTriangleAngles() {
// Clear existing triangle angles if any
TriangleAngles.clear();
// Calculate angles for each vertex of the triangle
for (size_t i = 0; i < TrianglePoints.size(); ++i) {
// Indices for the current point and observed angle
size_t next = (i + 1) % TrianglePoints.size(); // Next point index (wraps around)
size_t prev = (i == 0) ? TrianglePoints.size() - 1 : i - 1; // Previous point index
// Calculate vectors for the sides of the triangle
double vx1 = TrianglePoints[prev].x - TrianglePoints[i].x;
double vy1 = TrianglePoints[prev].y - TrianglePoints[i].y;
double vx2 = TrianglePoints[next].x - TrianglePoints[i].x;
double vy2 = TrianglePoints[next].y - TrianglePoints[i].y;
std::cout << "vx1 is " << vx1 << "\n";
std::cout << "vy1 is " << vy1 << "\n";
std::cout << "vx2 is " << vx2 << "\n";
std::cout << "vy2 is " << vy2 << "\n";
comments
// Calculate angle using dot product and cross product
double crossProduct = vx1 * vy2 - vy1 * vx2;
double dotProduct = vx1 * vx2 + vy1 * vy2;
std::cout << "crossProduct is " << crossProduct << "\n";
std::cout << "dotProduct is " << dotProduct << "\n";
Radian AngleRadians;
//Radian AngleRadians2;
AngleRadians.rad = atan2(crossProduct, dotProduct);
std::cout << "AngleRadians.rad is " << AngleRadians.rad << "\n";
// Create Angle object and add to TriangleAngles
TriangleAngles.push_back(Angle(AngleRadians));
}
}
double cot(const double angle) {
return 1.0 / std::tan(angle);
}
//Calculate K values
void calcKvalues() {
for(std::size_t i = 0; const auto& Ang : TriangleAngles ) {
double Kvalue = 1.0 / ( cot(Ang.radians)- cot(ObservedAngles[i].radians) );
Kvalues.push_back(Kvalue);
i++;
}
for(std::size_t i =0; const auto& K : Kvalues) {
std::cout << "Kvalue" << i << " " << K << "\n";
i++;
}
}
void TotalK() {
//std::cout << "TotalK is " << TotalKvalues << "\n";
for(const auto K : Kvalues) {
TotalKvalues += K ;
}
std::cout << "TotalK is " << TotalKvalues << "\n";
}
public:
Point getOccupiedPt() const {
return OccupiedPt;
}
// Function to calculate occupied point using the Tienstra formula
void calculateOccupiedPoint() {
std::cout << "Entering calculateOccupiedPoint\n";
double SumKE = 0.0;
for(int i =0; const auto& ThePoint : TrianglePoints ) {
SumKE += Kvalues[i] * ThePoint.x;
i++;
}
std::cout << "SumKE is " << SumKE << "\n";
OccupiedPt.x = SumKE / TotalKvalues;
double SumKN = 0.0; // sum of Kvlaue * North Ordinate for each point
for(int i =0 ; const auto& ThePoint : TrianglePoints) {
SumKN += Kvalues[i] * ThePoint.y;
i++;
}
std::cout << "SumKN is " << SumKN << "\n";
OccupiedPt.y = SumKN / TotalKvalues;
}
// Function to calculate distance between two points
double calculateDistance(const Point& p1, const Point& p2) const {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
// Function to perform the resection calculation and display results
void performResection() {
// Calculate occupied point
calculateOccupiedPoint();
}
}; // End of resection class
int main() {
std::cout << std::fixed;
std::cout << std::setprecision(16);
// Define triangle vertices (PtA, PtB, PtC) and observed angles (alpha, beta, gamma) in degrees
const std::vector<Point> TrianglePointsIn = { Point(0.0, 0.0), Point(500.0, 866.0254037844386), Point(1000.0, 0.0) };
const std::vector<Angle> ObservedAnglesIn = { Angle(120, 0, 0.0), Angle(120, 0, 0.0), Angle(120, 0, 0.0) };
// Create a Resection object with the triangle vertices and observed angles
Resection resection(TrianglePointsIn, ObservedAnglesIn);
// Perform resection calculation and display results
resection.performResection();
Point result = resection.getOccupiedPt();
std::cout << "Occupied Point is " << result.x << " " << result.y << "\n";
// Calculate distances to each triangle vertex
for (size_t i = 0; i < TrianglePointsIn.size(); ++i) {
double dist = resection.calculateDistance(TrianglePointsIn[i], resection.getOccupiedPt() );
std::cout << "Distance to Pt" << static_cast<char>('A' + i) << ": " << dist << " units\n";
}
return 0;
}
|