Tifa's CP Library

:warning: line3d (src/code/geo3d/line3d.hpp)

Depends on

Required by

Code

#ifndef TIFALIBS_GEO3D_LINE3D
#define TIFALIBS_GEO3D_LINE3D

#include "point3d.hpp"

namespace tifa_libs::geo {

template <class FP>
struct line3d {
  point3d<FP> l, r;
  explicit constexpr line3d(point3d<FP> const &s, point3d<FP> const &t) : l(s), r(t) {}

  friend std::istream &operator>>(std::istream &is, line3d &l) { return is >> l.l >> l.r; }
  friend std::ostream &operator<<(std::ostream &os, line3d const &l) { return os << l.l << ' ' << l.r; }
};

}  // namespace tifa_libs::geo

#endif
#line 1 "src/code/geo3d/line3d.hpp"



#line 1 "src/code/geo3d/point3d.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/geo3d/point3d.hpp"

namespace tifa_libs::geo {

template <class FP>
struct point3d {
  FP x, y, z;
  explicit constexpr point3d(FP x = FP{}, FP y = FP{}, FP z = FP{}) : x(x), y(y), z(z) {}

  friend std::istream &operator>>(std::istream &is, point3d &p) { return is >> p.x >> p.y >> p.z; }
  friend std::ostream &operator<<(std::ostream &os, point3d const &p) { return os << p.x << ' ' << p.y << ' ' << p.z; }

  // s * r + t * (1 - r)
  friend constexpr point3d lerp(point3d const &s, point3d const &t, FP r) { return s * r + t * (1 - r); }

  friend constexpr point3d mid_point(point3d const &s, point3d const &t) { return lerp(s, t, .5); }

  constexpr point3d &operator+=(FP n) {
    this->x += n;
    this->y += n;
    this->z += n;
    return *this;
  }
  constexpr point3d operator+(FP n) const { return point3d(*this) += n; }
  constexpr point3d &operator-=(FP n) {
    this->x -= n;
    this->y -= n;
    this->z -= n;
    return *this;
  }
  constexpr point3d operator-(FP n) const { return point3d(*this) -= n; }
  constexpr point3d &operator*=(FP n) {
    this->x *= n;
    this->y *= n;
    this->z *= n;
    return *this;
  }
  constexpr point3d operator*(FP n) const { return point3d(*this) *= n; }
  constexpr point3d &operator/=(FP n) {
    this->x /= n;
    this->y /= n;
    this->z /= n;
    return *this;
  }
  constexpr point3d operator/(FP n) const { return point3d(*this) /= n; }

  constexpr point3d &operator+=(point3d const &p) {
    this->x += p.x;
    this->y += p.y;
    this->z += p.z;
    return *this;
  }
  constexpr point3d operator+(point3d const &p) const { return point3d(*this) += p; }
  constexpr point3d &operator-=(point3d const &p) {
    this->x -= p.x;
    this->y -= p.y;
    this->z -= p.z;
    return *this;
  }
  constexpr point3d operator-(point3d const &p) const { return point3d(*this) -= p; }

  constexpr point3d operator-() const { return point3d{-x, -y, -z}; }
  constexpr auto operator<=>(point3d const &p) const {
    if (auto c = comp(x, p.x); c) return c;
    if (auto c = comp(y, p.y); c) return c;
    return comp(z, p.z);
  }
  constexpr bool operator==(point3d const &p) const { return is_eq(x, p.x) && is_eq(y, p.y) && is_eq(z, p.z); }

  constexpr FP operator*(point3d const &p) const { return x * p.x + y * p.y + z * p.z; }
  constexpr point3d operator^(point3d const &p) const { return point3d{y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x}; }

  constexpr auto norm2() const { return x * x + y * y + z * z; }
  constexpr auto norm() const { return std::hypot(x, y, z); }

  constexpr point3d &do_unit() { return *this /= norm(); }

  constexpr point3d &do_rotx(FP theta) {
    FP y0 = y, z0 = z, ct = std::cos(theta), st = std::sin(theta);
    y = y0 * ct - z0 * st;
    z = y0 * st + z0 * ct;
    return *this;
  }
  constexpr point3d &do_roty(FP theta) {
    FP x0 = x, z0 = z, ct = std::cos(theta), st = std::sin(theta);
    z = z0 * ct - x0 * st;
    x = z0 * st + x0 * ct;
    return *this;
  }
  constexpr point3d &do_rotz(FP theta) {
    FP x0 = x, y0 = y, ct = std::cos(theta), st = std::sin(theta);
    x = x0 * ct - y0 * st;
    y = x0 * st + y0 * ct;
    return *this;
  }
  constexpr point3d &do_rot(point3d e, FP theta) {
    e.do_unit();
    FP a = e.x, b = e.y, c = e.z, x0 = x, y0 = y, z0 = z, ct = std::cos(theta), st = std::sin(theta);
    x = x0 * (ct + a * a * (1 - ct)) + y0 * (a * b * (1 - ct) - c * st) + z0 * (a * c * (1 - ct) + b * st);
    y = x0 * (a * b * (1 - ct) + c * st) + y0 * (ct + b * b * (1 - ct)) + z0 * (b * c * (1 - ct) - a * st);
    z = x0 * (a * c * (1 - ct) - b * st) + y0 * (b * c * (1 - ct) + a * st) + z0 * (ct + c * c * (1 - ct));
    return *this;
  }
};

}  // namespace tifa_libs::geo


#line 5 "src/code/geo3d/line3d.hpp"

namespace tifa_libs::geo {

template <class FP>
struct line3d {
  point3d<FP> l, r;
  explicit constexpr line3d(point3d<FP> const &s, point3d<FP> const &t) : l(s), r(t) {}

  friend std::istream &operator>>(std::istream &is, line3d &l) { return is >> l.l >> l.r; }
  friend std::ostream &operator<<(std::ostream &os, line3d const &l) { return os << l.l << ' ' << l.r; }
};

}  // namespace tifa_libs::geo


Back to top page