2013-07-28 22:08:34 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Utility functions, mostly various kinds of vector math (working on real
|
|
|
|
// numbers, not working on quantities in the symbolic algebra system).
|
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-03-27 09:53:51 +00:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
2018-07-14 10:35:29 +00:00
|
|
|
void SolveSpace::AssertFailure(const char *file, unsigned line, const char *function,
|
|
|
|
const char *condition, const char *message) {
|
|
|
|
std::string formattedMsg;
|
|
|
|
formattedMsg += ssprintf("File %s, line %u, function %s:\n", file, line, function);
|
|
|
|
formattedMsg += ssprintf("Assertion failed: %s.\n", condition);
|
|
|
|
formattedMsg += ssprintf("Message: %s.\n", message);
|
|
|
|
SolveSpace::Platform::FatalError(formattedMsg);
|
|
|
|
}
|
|
|
|
|
2015-11-06 08:40:12 +00:00
|
|
|
std::string SolveSpace::ssprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
int size = vsnprintf(NULL, 0, fmt, va);
|
2016-05-18 22:51:36 +00:00
|
|
|
ssassert(size >= 0, "vsnprintf could not encode string");
|
2015-11-06 08:40:12 +00:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
std::string result;
|
2016-10-11 23:01:20 +00:00
|
|
|
result.resize(size + 1);
|
2015-11-06 08:40:12 +00:00
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
vsnprintf(&result[0], size + 1, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2016-10-11 23:01:20 +00:00
|
|
|
result.resize(size);
|
2015-11-06 08:40:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-14 20:13:40 +00:00
|
|
|
char32_t utf8_iterator::operator*()
|
2015-11-05 19:39:27 +00:00
|
|
|
{
|
2016-02-14 20:13:40 +00:00
|
|
|
const uint8_t *it = (const uint8_t*) this->p;
|
|
|
|
char32_t result = *it;
|
2015-11-05 19:39:27 +00:00
|
|
|
|
2016-02-14 20:13:40 +00:00
|
|
|
if((result & 0x80) != 0) {
|
2015-11-05 19:39:27 +00:00
|
|
|
unsigned int mask = 0x40;
|
|
|
|
|
2016-02-14 20:13:40 +00:00
|
|
|
do {
|
|
|
|
result <<= 6;
|
|
|
|
unsigned int c = (*++it);
|
2015-11-05 19:39:27 +00:00
|
|
|
mask <<= 5;
|
2016-02-14 20:13:40 +00:00
|
|
|
result += c - 0x80;
|
|
|
|
} while((result & mask) != 0);
|
2015-11-05 19:39:27 +00:00
|
|
|
|
2016-02-14 20:13:40 +00:00
|
|
|
result &= mask - 1;
|
2015-11-05 19:39:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 20:13:40 +00:00
|
|
|
this->n = (const char*) (it + 1);
|
|
|
|
return result;
|
2009-09-18 08:14:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 07:50:05 +00:00
|
|
|
int64_t SolveSpace::GetMilliseconds()
|
|
|
|
{
|
|
|
|
auto timestamp = std::chrono::steady_clock::now().time_since_epoch();
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(timestamp).count();
|
|
|
|
}
|
|
|
|
|
2015-03-23 17:49:04 +00:00
|
|
|
void SolveSpace::MakeMatrix(double *mat,
|
|
|
|
double a11, double a12, double a13, double a14,
|
|
|
|
double a21, double a22, double a23, double a24,
|
|
|
|
double a31, double a32, double a33, double a34,
|
|
|
|
double a41, double a42, double a43, double a44)
|
2008-03-27 09:53:51 +00:00
|
|
|
{
|
|
|
|
mat[ 0] = a11;
|
|
|
|
mat[ 1] = a21;
|
|
|
|
mat[ 2] = a31;
|
|
|
|
mat[ 3] = a41;
|
|
|
|
mat[ 4] = a12;
|
|
|
|
mat[ 5] = a22;
|
|
|
|
mat[ 6] = a32;
|
|
|
|
mat[ 7] = a42;
|
|
|
|
mat[ 8] = a13;
|
|
|
|
mat[ 9] = a23;
|
|
|
|
mat[10] = a33;
|
|
|
|
mat[11] = a43;
|
|
|
|
mat[12] = a14;
|
|
|
|
mat[13] = a24;
|
|
|
|
mat[14] = a34;
|
|
|
|
mat[15] = a44;
|
|
|
|
}
|
|
|
|
|
2016-06-30 15:54:35 +00:00
|
|
|
void SolveSpace::MultMatrix(double *mata, double *matb, double *matr) {
|
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
for(int j = 0; j < 4; j++) {
|
|
|
|
double s = 0.0;
|
|
|
|
for(int k = 0; k < 4; k++) {
|
|
|
|
s += mata[k * 4 + j] * matb[i * 4 + k];
|
|
|
|
}
|
|
|
|
matr[i * 4 + j] = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-16 09:22:44 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
Eliminate blocking in Error() and Message() calls.
This serves two purposes.
First, we want to (some day) convert these messages into a less
obtrustive form, something like toaster notifications, such that they
don't interrupt workflow as harshly. That would, of course, be
nonblocking.
Second, some platforms, like Emscripten, do not support nested event
loops, and it's not possible to display a modal dialog on them
synchronously.
When making this commit, I've reviewed all Error() and Message()
calls to ensure that only some of the following is true for all
of them:
* The call is followed a break or return statement that exits
an UI entry point (e.g. an MenuX function);
* The call is followed by cleanup (in fact, in this case the new
behavior is better, since even with a synchronous modal dialog
we have to be reentrant);
* The message is an informational message only and nothing
unexpected will happen if the operation proceeds in background.
In general, all Error() calls already satisfied the above conditions,
although in some cases I changed control flow aroudn them to more
clearly show that. The Message() calls that didn't satisfy these
conditions were reworked into an asynchronous form.
There are three explicit RunModal() calls left that need to be
reworked into an async form.
2018-07-19 21:54:05 +00:00
|
|
|
// Format the string for our message box appropriately, and then display
|
2010-01-16 09:22:44 +00:00
|
|
|
// that string.
|
|
|
|
//-----------------------------------------------------------------------------
|
Eliminate blocking in Error() and Message() calls.
This serves two purposes.
First, we want to (some day) convert these messages into a less
obtrustive form, something like toaster notifications, such that they
don't interrupt workflow as harshly. That would, of course, be
nonblocking.
Second, some platforms, like Emscripten, do not support nested event
loops, and it's not possible to display a modal dialog on them
synchronously.
When making this commit, I've reviewed all Error() and Message()
calls to ensure that only some of the following is true for all
of them:
* The call is followed a break or return statement that exits
an UI entry point (e.g. an MenuX function);
* The call is followed by cleanup (in fact, in this case the new
behavior is better, since even with a synchronous modal dialog
we have to be reentrant);
* The message is an informational message only and nothing
unexpected will happen if the operation proceeds in background.
In general, all Error() calls already satisfied the above conditions,
although in some cases I changed control flow aroudn them to more
clearly show that. The Message() calls that didn't satisfy these
conditions were reworked into an asynchronous form.
There are three explicit RunModal() calls left that need to be
reworked into an async form.
2018-07-19 21:54:05 +00:00
|
|
|
static void MessageBox(const char *fmt, va_list va, bool error,
|
|
|
|
std::function<void()> onDismiss = std::function<void()>())
|
2010-01-16 09:22:44 +00:00
|
|
|
{
|
2018-07-17 15:00:46 +00:00
|
|
|
#ifndef LIBRARY
|
|
|
|
va_list va_size;
|
|
|
|
va_copy(va_size, va);
|
|
|
|
int size = vsnprintf(NULL, 0, fmt, va_size);
|
|
|
|
ssassert(size >= 0, "vsnprintf could not encode string");
|
|
|
|
va_end(va_size);
|
|
|
|
|
|
|
|
std::string text;
|
|
|
|
text.resize(size);
|
|
|
|
|
|
|
|
vsnprintf(&text[0], size + 1, fmt, va);
|
|
|
|
|
|
|
|
// Split message text using a heuristic for better presentation.
|
|
|
|
size_t separatorAt = 0;
|
|
|
|
while(separatorAt != std::string::npos) {
|
|
|
|
size_t dotAt = text.find('.', separatorAt + 1),
|
|
|
|
colonAt = text.find(':', separatorAt + 1);
|
|
|
|
separatorAt = min(dotAt, colonAt);
|
|
|
|
if(separatorAt == std::string::npos ||
|
|
|
|
(separatorAt + 1 < text.size() && isspace(text[separatorAt + 1]))) {
|
|
|
|
break;
|
2010-01-16 09:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 02:19:35 +00:00
|
|
|
std::string message = text;
|
2018-07-17 15:00:46 +00:00
|
|
|
std::string description;
|
2018-07-18 02:19:35 +00:00
|
|
|
if(separatorAt != std::string::npos) {
|
|
|
|
message = text.substr(0, separatorAt + 1);
|
|
|
|
if(separatorAt + 1 < text.size()) {
|
|
|
|
description = text.substr(separatorAt + 1);
|
|
|
|
}
|
2010-01-16 09:22:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:03:26 +00:00
|
|
|
if(description.length() > 0) {
|
|
|
|
std::string::iterator it = description.begin();
|
|
|
|
while(isspace(*it)) it++;
|
|
|
|
description = description.substr(it - description.begin());
|
|
|
|
}
|
2018-07-17 15:00:46 +00:00
|
|
|
|
|
|
|
Platform::MessageDialogRef dialog = CreateMessageDialog(SS.GW.window);
|
2019-09-10 03:23:31 +00:00
|
|
|
if (!dialog) {
|
|
|
|
if (error) {
|
|
|
|
fprintf(stderr, "Error: %s\n", message.c_str());
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Message: %s\n", message.c_str());
|
|
|
|
}
|
|
|
|
if(onDismiss) {
|
|
|
|
onDismiss();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-07-17 15:00:46 +00:00
|
|
|
using Platform::MessageDialog;
|
|
|
|
if(error) {
|
|
|
|
dialog->SetType(MessageDialog::Type::ERROR);
|
|
|
|
} else {
|
|
|
|
dialog->SetType(MessageDialog::Type::INFORMATION);
|
|
|
|
}
|
|
|
|
dialog->SetTitle(error ? C_("title", "Error") : C_("title", "Message"));
|
|
|
|
dialog->SetMessage(message);
|
|
|
|
if(!description.empty()) {
|
|
|
|
dialog->SetDescription(description);
|
|
|
|
}
|
|
|
|
dialog->AddButton(C_("button", "&OK"), MessageDialog::Response::OK,
|
|
|
|
/*isDefault=*/true);
|
Eliminate blocking in Error() and Message() calls.
This serves two purposes.
First, we want to (some day) convert these messages into a less
obtrustive form, something like toaster notifications, such that they
don't interrupt workflow as harshly. That would, of course, be
nonblocking.
Second, some platforms, like Emscripten, do not support nested event
loops, and it's not possible to display a modal dialog on them
synchronously.
When making this commit, I've reviewed all Error() and Message()
calls to ensure that only some of the following is true for all
of them:
* The call is followed a break or return statement that exits
an UI entry point (e.g. an MenuX function);
* The call is followed by cleanup (in fact, in this case the new
behavior is better, since even with a synchronous modal dialog
we have to be reentrant);
* The message is an informational message only and nothing
unexpected will happen if the operation proceeds in background.
In general, all Error() calls already satisfied the above conditions,
although in some cases I changed control flow aroudn them to more
clearly show that. The Message() calls that didn't satisfy these
conditions were reworked into an asynchronous form.
There are three explicit RunModal() calls left that need to be
reworked into an async form.
2018-07-19 21:54:05 +00:00
|
|
|
|
|
|
|
dialog->onResponse = [=](MessageDialog::Response _response) {
|
|
|
|
if(onDismiss) {
|
|
|
|
onDismiss();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
dialog->ShowModal();
|
2018-07-17 15:00:46 +00:00
|
|
|
#endif
|
2010-01-16 09:22:44 +00:00
|
|
|
}
|
2018-07-17 15:00:46 +00:00
|
|
|
void SolveSpace::Error(const char *fmt, ...)
|
2010-01-16 09:22:44 +00:00
|
|
|
{
|
|
|
|
va_list f;
|
2018-07-17 15:00:46 +00:00
|
|
|
va_start(f, fmt);
|
|
|
|
MessageBox(fmt, f, /*error=*/true);
|
2010-01-16 09:22:44 +00:00
|
|
|
va_end(f);
|
|
|
|
}
|
2018-07-17 15:00:46 +00:00
|
|
|
void SolveSpace::Message(const char *fmt, ...)
|
2010-01-16 09:22:44 +00:00
|
|
|
{
|
|
|
|
va_list f;
|
2018-07-17 15:00:46 +00:00
|
|
|
va_start(f, fmt);
|
|
|
|
MessageBox(fmt, f, /*error=*/false);
|
2010-01-16 09:22:44 +00:00
|
|
|
va_end(f);
|
|
|
|
}
|
Eliminate blocking in Error() and Message() calls.
This serves two purposes.
First, we want to (some day) convert these messages into a less
obtrustive form, something like toaster notifications, such that they
don't interrupt workflow as harshly. That would, of course, be
nonblocking.
Second, some platforms, like Emscripten, do not support nested event
loops, and it's not possible to display a modal dialog on them
synchronously.
When making this commit, I've reviewed all Error() and Message()
calls to ensure that only some of the following is true for all
of them:
* The call is followed a break or return statement that exits
an UI entry point (e.g. an MenuX function);
* The call is followed by cleanup (in fact, in this case the new
behavior is better, since even with a synchronous modal dialog
we have to be reentrant);
* The message is an informational message only and nothing
unexpected will happen if the operation proceeds in background.
In general, all Error() calls already satisfied the above conditions,
although in some cases I changed control flow aroudn them to more
clearly show that. The Message() calls that didn't satisfy these
conditions were reworked into an asynchronous form.
There are three explicit RunModal() calls left that need to be
reworked into an async form.
2018-07-19 21:54:05 +00:00
|
|
|
void SolveSpace::MessageAndRun(std::function<void()> onDismiss, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list f;
|
|
|
|
va_start(f, fmt);
|
|
|
|
MessageBox(fmt, f, /*error=*/false, onDismiss);
|
|
|
|
va_end(f);
|
|
|
|
}
|
2010-01-16 09:22:44 +00:00
|
|
|
|
2009-10-21 04:46:01 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Solve a mostly banded matrix. In a given row, there are LEFT_OF_DIAG
|
|
|
|
// elements to the left of the diagonal element, and RIGHT_OF_DIAG elements to
|
|
|
|
// the right (so that the total band width is LEFT_OF_DIAG + RIGHT_OF_DIAG + 1).
|
|
|
|
// There also may be elements in the last two columns of any row. We solve
|
|
|
|
// without pivoting.
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-05 05:54:05 +00:00
|
|
|
void BandedMatrix::Solve() {
|
2009-10-21 04:46:01 +00:00
|
|
|
int i, ip, j, jp;
|
|
|
|
double temp;
|
|
|
|
|
|
|
|
// Reduce the matrix to upper triangular form.
|
|
|
|
for(i = 0; i < n; i++) {
|
|
|
|
for(ip = i+1; ip < n && ip <= (i + LEFT_OF_DIAG); ip++) {
|
|
|
|
temp = A[ip][i]/A[i][i];
|
|
|
|
|
|
|
|
for(jp = i; jp < (n - 2) && jp <= (i + RIGHT_OF_DIAG); jp++) {
|
|
|
|
A[ip][jp] -= temp*(A[i][jp]);
|
|
|
|
}
|
|
|
|
A[ip][n-2] -= temp*(A[i][n-2]);
|
|
|
|
A[ip][n-1] -= temp*(A[i][n-1]);
|
|
|
|
|
|
|
|
B[ip] -= temp*B[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And back-substitute.
|
|
|
|
for(i = n - 1; i >= 0; i--) {
|
|
|
|
temp = B[i];
|
|
|
|
|
|
|
|
if(i < n-1) temp -= X[n-1]*A[i][n-1];
|
|
|
|
if(i < n-2) temp -= X[n-2]*A[i][n-2];
|
|
|
|
|
|
|
|
for(j = min(n - 3, i + RIGHT_OF_DIAG); j > i; j--) {
|
|
|
|
temp -= X[j]*A[i][j];
|
|
|
|
}
|
|
|
|
X[i] = temp / A[i][i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 23:04:45 +00:00
|
|
|
const Quaternion Quaternion::IDENTITY = { 1, 0, 0, 0 };
|
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
Quaternion Quaternion::From(double w, double vx, double vy, double vz) {
|
2008-04-18 11:11:48 +00:00
|
|
|
Quaternion q;
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = w;
|
|
|
|
q.vx = vx;
|
|
|
|
q.vy = vy;
|
|
|
|
q.vz = vz;
|
2008-04-18 11:11:48 +00:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2008-06-02 03:31:37 +00:00
|
|
|
Quaternion Quaternion::From(hParam w, hParam vx, hParam vy, hParam vz) {
|
|
|
|
Quaternion q;
|
2009-04-19 05:53:16 +00:00
|
|
|
q.w = SK.GetParam(w )->val;
|
|
|
|
q.vx = SK.GetParam(vx)->val;
|
|
|
|
q.vy = SK.GetParam(vy)->val;
|
|
|
|
q.vz = SK.GetParam(vz)->val;
|
2008-06-02 03:31:37 +00:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2009-04-29 02:42:44 +00:00
|
|
|
Quaternion Quaternion::From(Vector axis, double dtheta) {
|
|
|
|
Quaternion q;
|
|
|
|
double c = cos(dtheta / 2), s = sin(dtheta / 2);
|
|
|
|
axis = axis.WithMagnitude(s);
|
|
|
|
q.w = c;
|
|
|
|
q.vx = axis.x;
|
|
|
|
q.vy = axis.y;
|
|
|
|
q.vz = axis.z;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
Quaternion Quaternion::From(Vector u, Vector v)
|
2008-04-18 11:11:48 +00:00
|
|
|
{
|
|
|
|
Vector n = u.Cross(v);
|
|
|
|
|
|
|
|
Quaternion q;
|
2008-05-03 03:33:35 +00:00
|
|
|
double s, tr = 1 + u.x + v.y + n.z;
|
|
|
|
if(tr > 1e-4) {
|
|
|
|
s = 2*sqrt(tr);
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = s/4;
|
|
|
|
q.vx = (v.z - n.y)/s;
|
|
|
|
q.vy = (n.x - u.z)/s;
|
|
|
|
q.vz = (u.y - v.x)/s;
|
2008-05-03 03:33:35 +00:00
|
|
|
} else {
|
2008-02-10 13:34:32 +00:00
|
|
|
if(u.x > v.y && u.x > n.z) {
|
2008-05-03 03:33:35 +00:00
|
|
|
s = 2*sqrt(1 + u.x - v.y - n.z);
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = (v.z - n.y)/s;
|
|
|
|
q.vx = s/4;
|
|
|
|
q.vy = (u.y + v.x)/s;
|
|
|
|
q.vz = (n.x + u.z)/s;
|
2008-02-10 13:34:32 +00:00
|
|
|
} else if(v.y > n.z) {
|
2008-05-03 03:33:35 +00:00
|
|
|
s = 2*sqrt(1 - u.x + v.y - n.z);
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = (n.x - u.z)/s;
|
|
|
|
q.vx = (u.y + v.x)/s;
|
|
|
|
q.vy = s/4;
|
|
|
|
q.vz = (v.z + n.y)/s;
|
2008-02-10 13:34:32 +00:00
|
|
|
} else {
|
2008-05-03 03:33:35 +00:00
|
|
|
s = 2*sqrt(1 - u.x - v.y + n.z);
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = (u.y - v.x)/s;
|
|
|
|
q.vx = (n.x + u.z)/s;
|
|
|
|
q.vy = (v.z + n.y)/s;
|
|
|
|
q.vz = s/4;
|
2008-02-10 13:34:32 +00:00
|
|
|
}
|
2008-05-03 03:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return q.WithMagnitude(1);
|
2008-04-18 11:11:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::Plus(Quaternion b) const {
|
2008-04-18 11:11:48 +00:00
|
|
|
Quaternion q;
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = w + b.w;
|
|
|
|
q.vx = vx + b.vx;
|
|
|
|
q.vy = vy + b.vy;
|
|
|
|
q.vz = vz + b.vz;
|
2008-04-18 11:11:48 +00:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::Minus(Quaternion b) const {
|
2008-04-18 11:11:48 +00:00
|
|
|
Quaternion q;
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = w - b.w;
|
|
|
|
q.vx = vx - b.vx;
|
|
|
|
q.vy = vy - b.vy;
|
|
|
|
q.vz = vz - b.vz;
|
2008-04-18 11:11:48 +00:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::ScaledBy(double s) const {
|
2008-04-18 11:11:48 +00:00
|
|
|
Quaternion q;
|
2008-05-05 06:18:01 +00:00
|
|
|
q.w = w*s;
|
|
|
|
q.vx = vx*s;
|
|
|
|
q.vy = vy*s;
|
|
|
|
q.vz = vz*s;
|
2008-04-18 11:11:48 +00:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
double Quaternion::Magnitude() const {
|
2008-05-05 06:18:01 +00:00
|
|
|
return sqrt(w*w + vx*vx + vy*vy + vz*vz);
|
2008-04-09 08:39:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::WithMagnitude(double s) const {
|
2008-04-18 11:11:48 +00:00
|
|
|
return ScaledBy(s/Magnitude());
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Quaternion::RotationU() const {
|
2008-04-09 08:39:01 +00:00
|
|
|
Vector v;
|
2008-05-05 06:18:01 +00:00
|
|
|
v.x = w*w + vx*vx - vy*vy - vz*vz;
|
|
|
|
v.y = 2*w *vz + 2*vx*vy;
|
|
|
|
v.z = 2*vx*vz - 2*w *vy;
|
2008-04-09 08:39:01 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Quaternion::RotationV() const {
|
2008-04-09 08:39:01 +00:00
|
|
|
Vector v;
|
2008-05-05 06:18:01 +00:00
|
|
|
v.x = 2*vx*vy - 2*w*vz;
|
|
|
|
v.y = w*w - vx*vx + vy*vy - vz*vz;
|
|
|
|
v.z = 2*w*vx + 2*vy*vz;
|
2008-04-09 08:39:01 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Quaternion::RotationN() const {
|
2008-06-06 07:50:08 +00:00
|
|
|
Vector v;
|
|
|
|
v.x = 2*w*vy + 2*vx*vz;
|
|
|
|
v.y = 2*vy*vz - 2*w*vx;
|
|
|
|
v.z = w*w - vx*vx - vy*vy + vz*vz;
|
|
|
|
return v;
|
2008-05-05 06:18:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Quaternion::Rotate(Vector p) const {
|
2008-05-11 06:09:46 +00:00
|
|
|
// Express the point in the new basis
|
|
|
|
return (RotationU().ScaledBy(p.x)).Plus(
|
|
|
|
RotationV().ScaledBy(p.y)).Plus(
|
|
|
|
RotationN().ScaledBy(p.z));
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::Inverse() const {
|
2008-05-14 14:23:58 +00:00
|
|
|
Quaternion r;
|
|
|
|
r.w = w;
|
|
|
|
r.vx = -vx;
|
|
|
|
r.vy = -vy;
|
|
|
|
r.vz = -vz;
|
|
|
|
return r.WithMagnitude(1); // not that the normalize should be reqd
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::ToThe(double p) const {
|
2008-07-02 09:52:32 +00:00
|
|
|
// Avoid division by zero, or arccos of something not in its domain
|
|
|
|
if(w >= (1 - 1e-6)) {
|
|
|
|
return From(1, 0, 0, 0);
|
|
|
|
} else if(w <= (-1 + 1e-6)) {
|
|
|
|
return From(-1, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2008-05-14 14:23:58 +00:00
|
|
|
Quaternion r;
|
2008-06-01 08:45:11 +00:00
|
|
|
Vector axis = Vector::From(vx, vy, vz);
|
2008-05-14 14:23:58 +00:00
|
|
|
double theta = acos(w); // okay, since magnitude is 1, so -1 <= w <= 1
|
|
|
|
theta *= p;
|
|
|
|
r.w = cos(theta);
|
|
|
|
axis = axis.WithMagnitude(sin(theta));
|
|
|
|
r.vx = axis.x;
|
|
|
|
r.vy = axis.y;
|
|
|
|
r.vz = axis.z;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::Times(Quaternion b) const {
|
2008-05-11 06:09:46 +00:00
|
|
|
double sa = w, sb = b.w;
|
|
|
|
Vector va = { vx, vy, vz };
|
|
|
|
Vector vb = { b.vx, b.vy, b.vz };
|
|
|
|
|
|
|
|
Quaternion r;
|
|
|
|
r.w = sa*sb - va.Dot(vb);
|
|
|
|
Vector vr = vb.ScaledBy(sa).Plus(
|
|
|
|
va.ScaledBy(sb).Plus(
|
|
|
|
va.Cross(vb)));
|
|
|
|
r.vx = vr.x;
|
|
|
|
r.vy = vr.y;
|
|
|
|
r.vz = vr.z;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Quaternion Quaternion::Mirror() const {
|
2009-10-09 12:57:10 +00:00
|
|
|
Vector u = RotationU(),
|
|
|
|
v = RotationV();
|
2009-12-15 12:26:22 +00:00
|
|
|
u = u.ScaledBy(-1);
|
|
|
|
v = v.ScaledBy(-1);
|
2009-10-09 12:57:10 +00:00
|
|
|
return Quaternion::From(u, v);
|
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2008-06-01 08:45:11 +00:00
|
|
|
Vector Vector::From(hParam x, hParam y, hParam z) {
|
2008-06-01 08:29:59 +00:00
|
|
|
Vector v;
|
2009-04-19 05:53:16 +00:00
|
|
|
v.x = SK.GetParam(x)->val;
|
|
|
|
v.y = SK.GetParam(y)->val;
|
|
|
|
v.z = SK.GetParam(z)->val;
|
2008-06-01 08:29:59 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
bool Vector::EqualsExactly(Vector v) const {
|
2013-10-21 21:29:25 +00:00
|
|
|
return EXACT(x == v.x &&
|
|
|
|
y == v.y &&
|
|
|
|
z == v.z);
|
2008-04-25 07:04:09 +00:00
|
|
|
}
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
double Vector::DirectionCosineWith(Vector b) const {
|
2009-06-22 06:22:30 +00:00
|
|
|
Vector a = this->WithMagnitude(1);
|
|
|
|
b = b.WithMagnitude(1);
|
|
|
|
return a.Dot(b);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::Normal(int which) const {
|
2008-04-01 10:48:44 +00:00
|
|
|
Vector n;
|
|
|
|
|
|
|
|
// Arbitrarily choose one vector that's normal to us, pivoting
|
|
|
|
// appropriately.
|
|
|
|
double xa = fabs(x), ya = fabs(y), za = fabs(z);
|
2008-07-08 06:30:13 +00:00
|
|
|
if(this->Equals(Vector::From(0, 0, 1))) {
|
|
|
|
// Make DXFs exported in the XY plane work nicely...
|
|
|
|
n = Vector::From(1, 0, 0);
|
|
|
|
} else if(xa < ya && xa < za) {
|
2008-04-01 10:48:44 +00:00
|
|
|
n.x = 0;
|
|
|
|
n.y = z;
|
|
|
|
n.z = -y;
|
2008-07-08 06:30:13 +00:00
|
|
|
} else if(ya < za) {
|
|
|
|
n.x = -z;
|
2008-04-01 10:48:44 +00:00
|
|
|
n.y = 0;
|
|
|
|
n.z = x;
|
2008-07-08 06:30:13 +00:00
|
|
|
} else {
|
2008-04-01 10:48:44 +00:00
|
|
|
n.x = y;
|
|
|
|
n.y = -x;
|
2008-07-08 06:30:13 +00:00
|
|
|
n.z = 0;
|
|
|
|
}
|
2008-04-01 10:48:44 +00:00
|
|
|
|
|
|
|
if(which == 0) {
|
|
|
|
// That's the vector we return.
|
|
|
|
} else if(which == 1) {
|
|
|
|
n = this->Cross(n);
|
2016-05-18 22:51:36 +00:00
|
|
|
} else ssassert(false, "Unexpected vector normal index");
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-14 10:28:32 +00:00
|
|
|
n = n.WithMagnitude(1);
|
2008-04-01 10:48:44 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::RotatedAbout(Vector orig, Vector axis, double theta) const {
|
2008-06-06 11:35:28 +00:00
|
|
|
Vector r = this->Minus(orig);
|
|
|
|
r = r.RotatedAbout(axis, theta);
|
|
|
|
return r.Plus(orig);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::RotatedAbout(Vector axis, double theta) const {
|
2008-03-27 09:53:51 +00:00
|
|
|
double c = cos(theta);
|
|
|
|
double s = sin(theta);
|
|
|
|
|
2008-05-03 03:33:35 +00:00
|
|
|
axis = axis.WithMagnitude(1);
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
Vector r;
|
|
|
|
|
|
|
|
r.x = (x)*(c + (1 - c)*(axis.x)*(axis.x)) +
|
|
|
|
(y)*((1 - c)*(axis.x)*(axis.y) - s*(axis.z)) +
|
|
|
|
(z)*((1 - c)*(axis.x)*(axis.z) + s*(axis.y));
|
|
|
|
|
|
|
|
r.y = (x)*((1 - c)*(axis.y)*(axis.x) + s*(axis.z)) +
|
|
|
|
(y)*(c + (1 - c)*(axis.y)*(axis.y)) +
|
|
|
|
(z)*((1 - c)*(axis.y)*(axis.z) - s*(axis.x));
|
|
|
|
|
|
|
|
r.z = (x)*((1 - c)*(axis.z)*(axis.x) - s*(axis.y)) +
|
|
|
|
(y)*((1 - c)*(axis.z)*(axis.y) + s*(axis.x)) +
|
|
|
|
(z)*(c + (1 - c)*(axis.z)*(axis.z));
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::DotInToCsys(Vector u, Vector v, Vector n) const {
|
2008-06-21 10:18:20 +00:00
|
|
|
Vector r = {
|
|
|
|
this->Dot(u),
|
|
|
|
this->Dot(v),
|
|
|
|
this->Dot(n)
|
|
|
|
};
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::ScaleOutOfCsys(Vector u, Vector v, Vector n) const {
|
2008-06-21 10:18:20 +00:00
|
|
|
Vector r = u.ScaledBy(x).Plus(
|
|
|
|
v.ScaledBy(y).Plus(
|
|
|
|
n.ScaledBy(z)));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-03-29 00:30:52 +00:00
|
|
|
Vector Vector::InPerspective(Vector u, Vector v, Vector n,
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector origin, double cameraTan) const
|
2009-03-17 16:33:46 +00:00
|
|
|
{
|
|
|
|
Vector r = this->Minus(origin);
|
|
|
|
r = r.DotInToCsys(u, v, n);
|
|
|
|
// yes, minus; we are assuming a csys where u cross v equals n, backwards
|
|
|
|
// from the display stuff
|
|
|
|
double w = (1 - r.z*cameraTan);
|
|
|
|
r = r.ScaledBy(1/w);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
double Vector::DistanceToLine(Vector p0, Vector dp) const {
|
2008-06-06 11:35:28 +00:00
|
|
|
double m = dp.Magnitude();
|
|
|
|
return ((this->Minus(p0)).Cross(dp)).Magnitude() / m;
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:54:21 +00:00
|
|
|
double Vector::DistanceToPlane(Vector normal, Vector origin) const {
|
|
|
|
return this->Dot(normal) - origin.Dot(normal);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
bool Vector::OnLineSegment(Vector a, Vector b, double tol) const {
|
2009-06-21 09:02:36 +00:00
|
|
|
if(this->Equals(a, tol) || this->Equals(b, tol)) return true;
|
2009-06-04 03:59:40 +00:00
|
|
|
|
2008-07-06 07:56:24 +00:00
|
|
|
Vector d = b.Minus(a);
|
|
|
|
|
|
|
|
double m = d.MagSquared();
|
|
|
|
double distsq = ((this->Minus(a)).Cross(d)).MagSquared() / m;
|
|
|
|
|
2009-06-21 09:02:36 +00:00
|
|
|
if(distsq >= tol*tol) return false;
|
2008-07-06 07:56:24 +00:00
|
|
|
|
2019-09-20 01:09:25 +00:00
|
|
|
double t = (this->Minus(a)).DivProjected(d);
|
2009-06-04 03:59:40 +00:00
|
|
|
// On-endpoint already tested
|
2008-07-06 07:56:24 +00:00
|
|
|
if(t < 0 || t > 1) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::ClosestPointOnLine(Vector p0, Vector dp) const {
|
2008-06-14 11:16:14 +00:00
|
|
|
dp = dp.WithMagnitude(1);
|
|
|
|
// this, p0, and (p0+dp) define a plane; the min distance is in
|
|
|
|
// that plane, so calculate its normal
|
|
|
|
Vector pn = (this->Minus(p0)).Cross(dp);
|
|
|
|
// The minimum distance line is in that plane, perpendicular
|
|
|
|
// to the line
|
|
|
|
Vector n = pn.Cross(dp);
|
|
|
|
|
|
|
|
// Calculate the actual distance
|
|
|
|
double d = (dp.Cross(p0.Minus(*this))).Magnitude();
|
|
|
|
return this->Plus(n.WithMagnitude(d));
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::WithMagnitude(double v) const {
|
2008-04-14 10:28:32 +00:00
|
|
|
double m = Magnitude();
|
2013-10-21 21:29:25 +00:00
|
|
|
if(EXACT(m == 0)) {
|
2009-06-10 08:04:35 +00:00
|
|
|
// We can do a zero vector with zero magnitude, but not any other cases.
|
|
|
|
if(fabs(v) > 1e-100) {
|
2009-07-20 01:47:59 +00:00
|
|
|
dbp("Vector::WithMagnitude(%g) of zero vector!", v);
|
2009-06-10 08:04:35 +00:00
|
|
|
}
|
|
|
|
return From(0, 0, 0);
|
2008-04-14 10:28:32 +00:00
|
|
|
} else {
|
2008-05-26 06:23:05 +00:00
|
|
|
return ScaledBy(v/m);
|
2008-04-14 10:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::ProjectVectorInto(hEntity wrkpl) const {
|
2009-04-20 07:30:09 +00:00
|
|
|
EntityBase *w = SK.GetEntity(wrkpl);
|
2008-05-08 07:30:30 +00:00
|
|
|
Vector u = w->Normal()->NormalU();
|
|
|
|
Vector v = w->Normal()->NormalV();
|
2008-05-17 11:15:14 +00:00
|
|
|
|
|
|
|
double up = this->Dot(u);
|
|
|
|
double vp = this->Dot(v);
|
|
|
|
|
|
|
|
return (u.ScaledBy(up)).Plus(v.ScaledBy(vp));
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::ProjectInto(hEntity wrkpl) const {
|
2009-04-20 07:30:09 +00:00
|
|
|
EntityBase *w = SK.GetEntity(wrkpl);
|
2008-05-08 07:30:30 +00:00
|
|
|
Vector p0 = w->WorkplaneGetOffset();
|
|
|
|
|
|
|
|
Vector f = this->Minus(p0);
|
|
|
|
|
2008-05-17 11:15:14 +00:00
|
|
|
return p0.Plus(f.ProjectVectorInto(wrkpl));
|
2008-05-08 07:30:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Point2d Vector::Project2d(Vector u, Vector v) const {
|
2008-05-12 10:01:44 +00:00
|
|
|
Point2d p;
|
|
|
|
p.x = this->Dot(u);
|
|
|
|
p.y = this->Dot(v);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Point2d Vector::ProjectXy() const {
|
2009-02-01 05:13:43 +00:00
|
|
|
Point2d p;
|
|
|
|
p.x = x;
|
|
|
|
p.y = y;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector4 Vector::Project4d() const {
|
2009-04-14 04:19:23 +00:00
|
|
|
return Vector4::From(1, x, y, z);
|
|
|
|
}
|
|
|
|
|
2019-09-20 01:09:25 +00:00
|
|
|
double Vector::DivProjected(Vector delta) const {
|
|
|
|
return (x*delta.x + y*delta.y + z*delta.z)
|
|
|
|
/ (delta.x*delta.x + delta.y*delta.y + delta.z*delta.z);
|
2008-05-08 08:12:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::ClosestOrtho() const {
|
2008-02-10 13:34:32 +00:00
|
|
|
double mx = fabs(x), my = fabs(y), mz = fabs(z);
|
2008-05-11 10:40:37 +00:00
|
|
|
|
2008-02-10 13:34:32 +00:00
|
|
|
if(mx > my && mx > mz) {
|
2008-06-01 08:45:11 +00:00
|
|
|
return From((x > 0) ? 1 : -1, 0, 0);
|
2008-02-10 13:34:32 +00:00
|
|
|
} else if(my > mz) {
|
2008-06-01 08:45:11 +00:00
|
|
|
return From(0, (y > 0) ? 1 : -1, 0);
|
2008-02-10 13:34:32 +00:00
|
|
|
} else {
|
2008-06-01 08:45:11 +00:00
|
|
|
return From(0, 0, (z > 0) ? 1 : -1);
|
2008-02-10 13:34:32 +00:00
|
|
|
}
|
2008-05-11 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector::ClampWithin(double minv, double maxv) const {
|
2010-07-21 05:04:03 +00:00
|
|
|
Vector ret = *this;
|
|
|
|
|
|
|
|
if(ret.x < minv) ret.x = minv;
|
|
|
|
if(ret.y < minv) ret.y = minv;
|
|
|
|
if(ret.z < minv) ret.z = minv;
|
|
|
|
|
|
|
|
if(ret.x > maxv) ret.x = maxv;
|
|
|
|
if(ret.y > maxv) ret.y = maxv;
|
|
|
|
if(ret.z > maxv) ret.z = maxv;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
bool Vector::OutsideAndNotOn(Vector maxv, Vector minv) const {
|
2009-01-21 05:04:38 +00:00
|
|
|
return (x > maxv.x + LENGTH_EPS) || (x < minv.x - LENGTH_EPS) ||
|
|
|
|
(y > maxv.y + LENGTH_EPS) || (y < minv.y - LENGTH_EPS) ||
|
|
|
|
(z > maxv.z + LENGTH_EPS) || (z < minv.z - LENGTH_EPS);
|
|
|
|
}
|
|
|
|
|
2009-01-27 07:59:58 +00:00
|
|
|
bool Vector::BoundingBoxesDisjoint(Vector amax, Vector amin,
|
|
|
|
Vector bmax, Vector bmin)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < 3; i++) {
|
|
|
|
if(amax.Element(i) < bmin.Element(i) - LENGTH_EPS) return true;
|
|
|
|
if(amin.Element(i) > bmax.Element(i) + LENGTH_EPS) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-08 10:59:57 +00:00
|
|
|
bool Vector::BoundingBoxIntersectsLine(Vector amax, Vector amin,
|
2016-05-25 12:08:19 +00:00
|
|
|
Vector p0, Vector p1, bool asSegment)
|
2009-03-08 10:59:57 +00:00
|
|
|
{
|
|
|
|
Vector dp = p1.Minus(p0);
|
|
|
|
double lp = dp.Magnitude();
|
|
|
|
dp = dp.ScaledBy(1.0/lp);
|
|
|
|
|
|
|
|
int i, a;
|
|
|
|
for(i = 0; i < 3; i++) {
|
|
|
|
int j = WRAP(i+1, 3), k = WRAP(i+2, 3);
|
|
|
|
if(lp*fabs(dp.Element(i)) < LENGTH_EPS) continue; // parallel to plane
|
|
|
|
|
|
|
|
for(a = 0; a < 2; a++) {
|
|
|
|
double d = (a == 0) ? amax.Element(i) : amin.Element(i);
|
|
|
|
// n dot (p0 + t*dp) = d
|
|
|
|
// (n dot p0) + t * (n dot dp) = d
|
|
|
|
double t = (d - p0.Element(i)) / dp.Element(i);
|
|
|
|
Vector p = p0.Plus(dp.ScaledBy(t));
|
|
|
|
|
2016-05-25 12:08:19 +00:00
|
|
|
if(asSegment && (t < -LENGTH_EPS || t > (lp+LENGTH_EPS))) continue;
|
2009-03-08 10:59:57 +00:00
|
|
|
|
|
|
|
if(p.Element(j) > amax.Element(j) + LENGTH_EPS) continue;
|
|
|
|
if(p.Element(k) > amax.Element(k) + LENGTH_EPS) continue;
|
|
|
|
|
|
|
|
if(p.Element(j) < amin.Element(j) - LENGTH_EPS) continue;
|
|
|
|
if(p.Element(k) < amin.Element(k) - LENGTH_EPS) continue;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-19 09:23:49 +00:00
|
|
|
Vector Vector::AtIntersectionOfPlanes(Vector n1, double d1,
|
|
|
|
Vector n2, double d2)
|
|
|
|
{
|
2015-03-29 00:30:52 +00:00
|
|
|
double det = (n1.Dot(n1))*(n2.Dot(n2)) -
|
2008-05-19 09:23:49 +00:00
|
|
|
(n1.Dot(n2))*(n1.Dot(n2));
|
|
|
|
double c1 = (d1*n2.Dot(n2) - d2*n1.Dot(n2))/det;
|
|
|
|
double c2 = (d2*n1.Dot(n1) - d1*n1.Dot(n2))/det;
|
2015-03-29 00:30:52 +00:00
|
|
|
|
2008-05-19 09:23:49 +00:00
|
|
|
return (n1.ScaledBy(c1)).Plus(n2.ScaledBy(c2));
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:21:59 +00:00
|
|
|
void Vector::ClosestPointBetweenLines(Vector a0, Vector da,
|
|
|
|
Vector b0, Vector db,
|
|
|
|
double *ta, double *tb)
|
2009-01-03 12:27:33 +00:00
|
|
|
{
|
2009-07-07 08:21:59 +00:00
|
|
|
// Make a semi-orthogonal coordinate system from those directions;
|
|
|
|
// note that dna and dnb need not be perpendicular.
|
2009-01-03 12:27:33 +00:00
|
|
|
Vector dn = da.Cross(db); // normal to both
|
|
|
|
Vector dna = dn.Cross(da); // normal to da
|
|
|
|
Vector dnb = dn.Cross(db); // normal to db
|
|
|
|
|
|
|
|
// At the intersection of the lines
|
|
|
|
// a0 + pa*da = b0 + pb*db (where pa, pb are scalar params)
|
|
|
|
// So dot this equation against dna and dnb to get two equations
|
|
|
|
// to solve for da and db
|
2009-07-07 08:21:59 +00:00
|
|
|
*tb = ((a0.Minus(b0)).Dot(dna))/(db.Dot(dna));
|
2015-03-29 00:30:52 +00:00
|
|
|
*ta = -((a0.Minus(b0)).Dot(dnb))/(da.Dot(dnb));
|
2009-07-07 08:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector Vector::AtIntersectionOfLines(Vector a0, Vector a1,
|
|
|
|
Vector b0, Vector b1,
|
|
|
|
bool *skew,
|
|
|
|
double *parama, double *paramb)
|
|
|
|
{
|
|
|
|
Vector da = a1.Minus(a0), db = b1.Minus(b0);
|
|
|
|
|
|
|
|
double pa, pb;
|
|
|
|
Vector::ClosestPointBetweenLines(a0, da, b0, db, &pa, &pb);
|
|
|
|
|
2009-01-21 05:04:38 +00:00
|
|
|
if(parama) *parama = pa;
|
|
|
|
if(paramb) *paramb = pb;
|
2009-01-03 12:27:33 +00:00
|
|
|
|
|
|
|
// And from either of those, we get the intersection point.
|
|
|
|
Vector pi = a0.Plus(da.ScaledBy(pa));
|
|
|
|
|
|
|
|
if(skew) {
|
|
|
|
// Check if the intersection points on each line are actually
|
|
|
|
// coincident...
|
|
|
|
if(pi.Equals(b0.Plus(db.ScaledBy(pb)))) {
|
|
|
|
*skew = false;
|
|
|
|
} else {
|
|
|
|
*skew = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pi;
|
|
|
|
}
|
|
|
|
|
2009-02-23 10:06:02 +00:00
|
|
|
Vector Vector::AtIntersectionOfPlaneAndLine(Vector n, double d,
|
|
|
|
Vector p0, Vector p1,
|
|
|
|
bool *parallel)
|
|
|
|
{
|
|
|
|
Vector dp = p1.Minus(p0);
|
|
|
|
|
|
|
|
if(fabs(n.Dot(dp)) < LENGTH_EPS) {
|
|
|
|
if(parallel) *parallel = true;
|
|
|
|
return Vector::From(0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parallel) *parallel = false;
|
|
|
|
|
|
|
|
// n dot (p0 + t*dp) = d
|
|
|
|
// (n dot p0) + t * (n dot dp) = d
|
|
|
|
double t = (d - n.Dot(p0)) / (n.Dot(dp));
|
|
|
|
|
|
|
|
return p0.Plus(dp.ScaledBy(t));
|
|
|
|
}
|
|
|
|
|
2009-02-27 13:04:36 +00:00
|
|
|
static double det2(double a1, double b1,
|
|
|
|
double a2, double b2)
|
|
|
|
{
|
|
|
|
return (a1*b2) - (b1*a2);
|
|
|
|
}
|
|
|
|
static double det3(double a1, double b1, double c1,
|
|
|
|
double a2, double b2, double c2,
|
|
|
|
double a3, double b3, double c3)
|
|
|
|
{
|
|
|
|
return a1*det2(b2, c2, b3, c3) -
|
|
|
|
b1*det2(a2, c2, a3, c3) +
|
|
|
|
c1*det2(a2, b2, a3, b3);
|
|
|
|
}
|
|
|
|
Vector Vector::AtIntersectionOfPlanes(Vector na, double da,
|
|
|
|
Vector nb, double db,
|
|
|
|
Vector nc, double dc,
|
|
|
|
bool *parallel)
|
|
|
|
{
|
|
|
|
double det = det3(na.x, na.y, na.z,
|
|
|
|
nb.x, nb.y, nb.z,
|
|
|
|
nc.x, nc.y, nc.z);
|
|
|
|
if(fabs(det) < 1e-10) { // arbitrary tolerance, not so good
|
|
|
|
*parallel = true;
|
|
|
|
return Vector::From(0, 0, 0);
|
|
|
|
}
|
|
|
|
*parallel = false;
|
|
|
|
|
|
|
|
double detx = det3(da, na.y, na.z,
|
|
|
|
db, nb.y, nb.z,
|
|
|
|
dc, nc.y, nc.z);
|
|
|
|
|
|
|
|
double dety = det3(na.x, da, na.z,
|
|
|
|
nb.x, db, nb.z,
|
|
|
|
nc.x, dc, nc.z);
|
|
|
|
|
|
|
|
double detz = det3(na.x, na.y, da,
|
|
|
|
nb.x, nb.y, db,
|
|
|
|
nc.x, nc.y, dc );
|
|
|
|
|
|
|
|
return Vector::From(detx/det, dety/det, detz/det);
|
|
|
|
}
|
|
|
|
|
2016-06-24 09:06:30 +00:00
|
|
|
size_t VectorHash::operator()(const Vector &v) const {
|
|
|
|
const size_t size = (size_t)pow(std::numeric_limits<size_t>::max(), 1.0 / 3.0) - 1;
|
|
|
|
const double eps = 4.0 * LENGTH_EPS;
|
|
|
|
|
|
|
|
double x = fabs(v.x) / eps;
|
|
|
|
double y = fabs(v.y) / eps;
|
|
|
|
double z = fabs(v.y) / eps;
|
|
|
|
|
|
|
|
size_t xs = size_t(fmod(x, (double)size));
|
|
|
|
size_t ys = size_t(fmod(y, (double)size));
|
|
|
|
size_t zs = size_t(fmod(z, (double)size));
|
|
|
|
|
|
|
|
return (zs * size + ys) * size + xs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VectorPred::operator()(Vector a, Vector b) const {
|
|
|
|
return a.Equals(b, LENGTH_EPS);
|
|
|
|
}
|
|
|
|
|
2009-04-14 04:19:23 +00:00
|
|
|
Vector4 Vector4::From(double w, double x, double y, double z) {
|
|
|
|
Vector4 ret;
|
|
|
|
ret.w = w;
|
|
|
|
ret.x = x;
|
|
|
|
ret.y = y;
|
|
|
|
ret.z = z;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector4 Vector4::From(double w, Vector v) {
|
|
|
|
return Vector4::From(w, w*v.x, w*v.y, w*v.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector4 Vector4::Blend(Vector4 a, Vector4 b, double t) {
|
|
|
|
return (a.ScaledBy(1 - t)).Plus(b.ScaledBy(t));
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector4 Vector4::Plus(Vector4 b) const {
|
2009-04-14 04:19:23 +00:00
|
|
|
return Vector4::From(w + b.w, x + b.x, y + b.y, z + b.z);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector4 Vector4::Minus(Vector4 b) const {
|
2009-04-14 04:19:23 +00:00
|
|
|
return Vector4::From(w - b.w, x - b.x, y - b.y, z - b.z);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector4 Vector4::ScaledBy(double s) const {
|
2009-04-14 04:19:23 +00:00
|
|
|
return Vector4::From(w*s, x*s, y*s, z*s);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector Vector4::PerspectiveProject() const {
|
2009-04-14 04:19:23 +00:00
|
|
|
return Vector::From(x / w, y / w, z / w);
|
|
|
|
}
|
|
|
|
|
2009-03-14 20:01:20 +00:00
|
|
|
Point2d Point2d::From(double x, double y) {
|
2016-02-03 13:00:52 +00:00
|
|
|
return { x, y };
|
2009-03-14 20:01:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 22:35:23 +00:00
|
|
|
Point2d Point2d::FromPolar(double r, double a) {
|
|
|
|
return { r * cos(a), r * sin(a) };
|
|
|
|
}
|
|
|
|
|
|
|
|
double Point2d::Angle() const {
|
|
|
|
double a = atan2(y, x);
|
|
|
|
return M_PI + remainder(a - M_PI, 2 * M_PI);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Point2d::AngleTo(const Point2d &p) const {
|
|
|
|
return p.Minus(*this).Angle();
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
Point2d Point2d::Plus(const Point2d &b) const {
|
|
|
|
return { x + b.x, y + b.y };
|
2008-04-12 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
Point2d Point2d::Minus(const Point2d &b) const {
|
|
|
|
return { x - b.x, y - b.y };
|
2008-04-12 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
Point2d Point2d::ScaledBy(double s) const {
|
|
|
|
return { x * s, y * s };
|
2008-04-12 14:12:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 01:09:25 +00:00
|
|
|
double Point2d::DivProjected(Point2d delta) const {
|
|
|
|
return (x*delta.x + y*delta.y) / (delta.x*delta.x + delta.y*delta.y);
|
2009-03-19 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 05:54:05 +00:00
|
|
|
double Point2d::MagSquared() const {
|
2009-02-23 10:06:02 +00:00
|
|
|
return x*x + y*y;
|
|
|
|
}
|
|
|
|
|
2016-05-05 05:54:05 +00:00
|
|
|
double Point2d::Magnitude() const {
|
2008-04-22 05:00:49 +00:00
|
|
|
return sqrt(x*x + y*y);
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
Point2d Point2d::WithMagnitude(double v) const {
|
2008-04-22 05:00:49 +00:00
|
|
|
double m = Magnitude();
|
2009-03-02 05:52:08 +00:00
|
|
|
if(m < 1e-20) {
|
|
|
|
dbp("!!! WithMagnitude() of zero vector");
|
2016-02-03 13:00:52 +00:00
|
|
|
return { v, 0 };
|
2008-04-22 05:00:49 +00:00
|
|
|
}
|
2016-02-03 13:00:52 +00:00
|
|
|
return { x * v / m, y * v / m };
|
2008-04-22 05:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
double Point2d::DistanceTo(const Point2d &p) const {
|
2008-04-12 14:12:26 +00:00
|
|
|
double dx = x - p.x;
|
|
|
|
double dy = y - p.y;
|
|
|
|
return sqrt(dx*dx + dy*dy);
|
|
|
|
}
|
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
double Point2d::Dot(Point2d p) const {
|
2009-02-01 05:13:43 +00:00
|
|
|
return x*p.x + y*p.y;
|
|
|
|
}
|
|
|
|
|
2016-05-25 12:08:19 +00:00
|
|
|
double Point2d::DistanceToLine(const Point2d &p0, const Point2d &dp, bool asSegment) const {
|
2008-04-12 14:12:26 +00:00
|
|
|
double m = dp.x*dp.x + dp.y*dp.y;
|
2009-02-01 13:01:28 +00:00
|
|
|
if(m < LENGTH_EPS*LENGTH_EPS) return VERY_POSITIVE;
|
2015-03-29 00:30:52 +00:00
|
|
|
|
2008-04-12 14:12:26 +00:00
|
|
|
// Let our line be p = p0 + t*dp, for a scalar t from 0 to 1
|
|
|
|
double t = (dp.x*(x - p0.x) + dp.y*(y - p0.y))/m;
|
|
|
|
|
2016-05-25 12:08:19 +00:00
|
|
|
if(asSegment) {
|
2016-05-05 09:03:30 +00:00
|
|
|
if(t < 0.0) return DistanceTo(p0);
|
|
|
|
if(t > 1.0) return DistanceTo(p0.Plus(dp));
|
2008-04-12 14:12:26 +00:00
|
|
|
}
|
2016-05-05 09:03:30 +00:00
|
|
|
Point2d closest = p0.Plus(dp.ScaledBy(t));
|
|
|
|
return DistanceTo(closest);
|
2008-04-12 14:12:26 +00:00
|
|
|
}
|
2008-04-11 11:13:47 +00:00
|
|
|
|
Abstract all (ex-OpenGL) drawing operations into a Canvas interface.
This has several desirable consequences:
* It is now possible to port SolveSpace to a later version of
OpenGL, such as OpenGLES 2, so that it runs on platforms that
only have that OpenGL version;
* The majority of geometry is now rendered without references to
the camera in C++ code, so a renderer can now submit it to
the video card once and re-rasterize with a different projection
matrix every time the projection is changed, avoiding expensive
reuploads;
* The DOGD (draw or get distance) interface is now
a straightforward Canvas implementation;
* There are no more direct references to SS.GW.(projection)
in sketch rendering code, which allows rendering to multiple
viewports;
* There are no more unnecessary framebuffer flips on CPU on Cocoa
and GTK;
* The platform-dependent GL code is now confined to rendergl1.cpp.
* The Microsoft and Apple headers required by it that are prone to
identifier conflicts are no longer included globally;
* The rendergl1.cpp implementation can now be omitted from
compilation to run SolveSpace headless or with a different
OpenGL version.
Note these implementation details of Canvas:
* GetCamera currently always returns a reference to the field
`Camera camera;`. This is so that a future renderer that caches
geometry in the video memory can define it as asserting, which
would provide assurance against code that could accidentally
put something projection-dependent in the cache;
* Line and triangle rendering is specified through a level of
indirection, hStroke and hFill. This is so that a future renderer
that batches geometry could cheaply group identical styles.
* DrawPixmap and DrawVectorText accept a (o,u,v) and not a matrix.
This is so that a future renderer into an output format that
uses 2d transforms (e.g. SVG) could easily derive those.
Some additional internal changes were required to enable this:
* Pixmap is now always passed as std::shared_ptr<{const ,}Pixmap>.
This is so that the renderer could cache uploaded textures
between API calls, which requires it to capture a (weak)
reference.
* The PlatformPathEqual function was properly extracted into
platform-specific code. This is so that the <windows.h> header
could be included only where needed (in platform/w32* as well
as rendergl1.cpp).
* The SBsp{2,3}::DebugDraw functions were removed. They can be
rewritten using the Canvas API if they are ever needed.
While no visual changes were originally intended, some minor fixes
happened anyway:
* The "emphasis" yellow line from top-left corner is now correctly
rendered much wider.
* The marquee rectangle is now pixel grid aligned.
* The hidden entities now do not clobber the depth buffer, removing
some minor artifacts.
* The workplane "tab" now scales with the font used to render
the workplane name.
* The workplane name font is now taken from the normals style.
* Workplane and constraint line stipple is insignificantly
different. This is so that it can reuse the existing stipple
codepaths; rendering of workplanes and constraints predates
those.
Some debug functionality was added:
* In graphics window, an fps counter that becomes red when
rendering under 60fps is drawn.
2016-05-31 00:55:13 +00:00
|
|
|
double Point2d::DistanceToLineSigned(const Point2d &p0, const Point2d &dp, bool asSegment) const {
|
|
|
|
double m = dp.x*dp.x + dp.y*dp.y;
|
|
|
|
if(m < LENGTH_EPS*LENGTH_EPS) return VERY_POSITIVE;
|
|
|
|
|
|
|
|
Point2d n = dp.Normal().WithMagnitude(1.0);
|
|
|
|
double dist = n.Dot(*this) - n.Dot(p0);
|
|
|
|
if(asSegment) {
|
|
|
|
// Let our line be p = p0 + t*dp, for a scalar t from 0 to 1
|
|
|
|
double t = (dp.x*(x - p0.x) + dp.y*(y - p0.y))/m;
|
|
|
|
double sign = (dist > 0.0) ? 1.0 : -1.0;
|
|
|
|
if(t < 0.0) return DistanceTo(p0) * sign;
|
|
|
|
if(t > 1.0) return DistanceTo(p0.Plus(dp)) * sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
2016-05-05 05:54:05 +00:00
|
|
|
Point2d Point2d::Normal() const {
|
2016-02-03 13:00:52 +00:00
|
|
|
return { y, -x };
|
2009-02-01 05:13:43 +00:00
|
|
|
}
|
2008-05-08 07:30:30 +00:00
|
|
|
|
2016-02-03 13:00:52 +00:00
|
|
|
bool Point2d::Equals(Point2d v, double tol) const {
|
2009-02-23 10:06:02 +00:00
|
|
|
double dx = v.x - x; if(dx < -tol || dx > tol) return false;
|
|
|
|
double dy = v.y - y; if(dy < -tol || dy > tol) return false;
|
|
|
|
|
|
|
|
return (this->Minus(v)).MagSquared() < tol*tol;
|
|
|
|
}
|
|
|
|
|
2016-02-18 09:53:31 +00:00
|
|
|
BBox BBox::From(const Vector &p0, const Vector &p1) {
|
|
|
|
BBox bbox;
|
|
|
|
bbox.minp.x = min(p0.x, p1.x);
|
|
|
|
bbox.minp.y = min(p0.y, p1.y);
|
|
|
|
bbox.minp.z = min(p0.z, p1.z);
|
|
|
|
|
|
|
|
bbox.maxp.x = max(p0.x, p1.x);
|
|
|
|
bbox.maxp.y = max(p0.y, p1.y);
|
|
|
|
bbox.maxp.z = max(p0.z, p1.z);
|
|
|
|
return bbox;
|
2016-01-23 08:05:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 03:44:38 +00:00
|
|
|
Vector BBox::GetOrigin() const { return minp.Plus(maxp.Minus(minp).ScaledBy(0.5)); }
|
2016-05-21 05:18:00 +00:00
|
|
|
Vector BBox::GetExtents() const { return maxp.Minus(minp).ScaledBy(0.5); }
|
2016-01-23 08:05:02 +00:00
|
|
|
|
|
|
|
void BBox::Include(const Vector &v, double r) {
|
|
|
|
minp.x = min(minp.x, v.x - r);
|
|
|
|
minp.y = min(minp.y, v.y - r);
|
|
|
|
minp.z = min(minp.z, v.z - r);
|
|
|
|
|
|
|
|
maxp.x = max(maxp.x, v.x + r);
|
|
|
|
maxp.y = max(maxp.y, v.y + r);
|
|
|
|
maxp.z = max(maxp.z, v.z + r);
|
|
|
|
}
|
|
|
|
|
2016-05-21 05:18:00 +00:00
|
|
|
bool BBox::Overlaps(const BBox &b1) const {
|
2016-01-23 08:05:02 +00:00
|
|
|
Vector t = b1.GetOrigin().Minus(GetOrigin());
|
|
|
|
Vector e = b1.GetExtents().Plus(GetExtents());
|
|
|
|
|
|
|
|
return fabs(t.x) < e.x && fabs(t.y) < e.y && fabs(t.z) < e.z;
|
|
|
|
}
|
2016-02-28 17:08:23 +00:00
|
|
|
|
2016-05-24 03:44:38 +00:00
|
|
|
bool BBox::Contains(const Point2d &p, double r) const {
|
|
|
|
return p.x >= (minp.x - r) &&
|
|
|
|
p.y >= (minp.y - r) &&
|
|
|
|
p.x <= (maxp.x + r) &&
|
|
|
|
p.y <= (maxp.y + r);
|
2016-02-28 17:08:23 +00:00
|
|
|
}
|
2016-07-21 20:27:53 +00:00
|
|
|
|
2016-10-13 16:43:13 +00:00
|
|
|
const std::vector<double>& SolveSpace::StipplePatternDashes(StipplePattern pattern) {
|
|
|
|
static bool initialized;
|
|
|
|
static std::vector<double> dashes[(size_t)StipplePattern::LAST + 1];
|
|
|
|
if(!initialized) {
|
|
|
|
// Inkscape ignores all elements that are exactly zero instead of drawing
|
|
|
|
// them as dots, so set those to 1e-6.
|
|
|
|
dashes[(size_t)StipplePattern::CONTINUOUS] =
|
|
|
|
{};
|
|
|
|
dashes[(size_t)StipplePattern::SHORT_DASH] =
|
|
|
|
{ 1.0, 2.0 };
|
|
|
|
dashes[(size_t)StipplePattern::DASH] =
|
|
|
|
{ 1.0, 1.0 };
|
|
|
|
dashes[(size_t)StipplePattern::DASH_DOT] =
|
|
|
|
{ 1.0, 0.5, 1e-6, 0.5 };
|
|
|
|
dashes[(size_t)StipplePattern::DASH_DOT_DOT] =
|
2017-08-01 15:04:28 +00:00
|
|
|
{ 1.0, 0.5, 1e-6, 0.5, 1e-6, 0.5 };
|
2016-10-13 16:43:13 +00:00
|
|
|
dashes[(size_t)StipplePattern::DOT] =
|
|
|
|
{ 1e-6, 0.5 };
|
|
|
|
dashes[(size_t)StipplePattern::LONG_DASH] =
|
|
|
|
{ 2.0, 0.5 };
|
|
|
|
dashes[(size_t)StipplePattern::FREEHAND] =
|
|
|
|
{ 1.0, 2.0 };
|
|
|
|
dashes[(size_t)StipplePattern::ZIGZAG] =
|
|
|
|
{ 1.0, 2.0 };
|
2016-07-21 20:27:53 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 16:43:13 +00:00
|
|
|
return dashes[(size_t)pattern];
|
|
|
|
}
|
|
|
|
|
|
|
|
double SolveSpace::StipplePatternLength(StipplePattern pattern) {
|
|
|
|
static bool initialized;
|
|
|
|
static double lengths[(size_t)StipplePattern::LAST + 1];
|
|
|
|
if(!initialized) {
|
|
|
|
for(size_t i = 0; i < (size_t)StipplePattern::LAST; i++) {
|
|
|
|
const std::vector<double> &dashes = StipplePatternDashes((StipplePattern)i);
|
|
|
|
double length = 0.0;
|
|
|
|
for(double dash : dashes) {
|
|
|
|
length += dash;
|
|
|
|
}
|
|
|
|
lengths[i] = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lengths[(size_t)pattern];
|
2016-07-21 20:27:53 +00:00
|
|
|
}
|