🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Unit.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "Dimension.hpp"
25#include "AstUtil/Object.hpp"
26#include "AstUtil/SharedPtr.hpp"
27#include "AstUtil/StringView.hpp"
28#include "AstUtil/Constants.h"
29#include <cmath>
30#include <string>
31#include <vector>
32#include <memory>
33
34AST_NAMESPACE_BEGIN
35
36
44class Unit;
45class Quantity;
46
47
48
49namespace units
50{
51 extern AST_UTIL_API Unit none;
52
53 extern AST_UTIL_API Unit mm;
54 extern AST_UTIL_API Unit cm;
55 extern AST_UTIL_API Unit dm;
56 extern AST_UTIL_API Unit m;
57 extern AST_UTIL_API Unit km;
58
59 extern AST_UTIL_API Unit in;
60 extern AST_UTIL_API Unit ft;
61 extern AST_UTIL_API Unit yd;
62 extern AST_UTIL_API Unit mi;
63
64
65
66 extern AST_UTIL_API Unit sec;
67 extern AST_UTIL_API Unit s;
68 extern AST_UTIL_API Unit ms;
69 extern AST_UTIL_API Unit minute;
70 extern AST_UTIL_API Unit hour;
71 extern AST_UTIL_API Unit h;
72 extern AST_UTIL_API Unit day;
73
74 extern AST_UTIL_API Unit kg;
75 extern AST_UTIL_API Unit g;
76 extern AST_UTIL_API Unit mg;
77 extern AST_UTIL_API Unit lb;
78
79 extern AST_UTIL_API Unit N;
80
81 extern AST_UTIL_API Unit deg;
82 extern AST_UTIL_API Unit rad;
83 extern AST_UTIL_API Unit arcsec;
84
85 extern AST_UTIL_API Unit A;
86
87 extern AST_UTIL_API Unit m2;
88
89 extern AST_UTIL_API Unit m3;
90 extern AST_UTIL_API Unit L;
91
92 extern AST_UTIL_API Unit Pa;
93
94 extern AST_UTIL_API Unit K;
95};
96
97
98
100AST_UTIL_API Unit aUnitMultiply(const Unit& unit1, const Unit& unit2);
101
103AST_UTIL_API Unit aUnitMultiply(const Unit& unit1, const Unit& unit2, StringView newname);
104
106AST_UTIL_API Unit aUnitDivide(const Unit& unit1, const Unit& unit2);
107
109AST_UTIL_API Unit aUnitDivide(const Unit& unit1, const Unit& unit2, StringView newname);
110
112AST_UTIL_API Unit aUnitPower(const Unit& unit, int exponent);
113
115AST_UTIL_API Unit aUnitPower(const Unit& unit, int exponent, StringView newname);
116
118AST_UTIL_API Unit aUnitInvert(const Unit& unit);
119
121AST_UTIL_API Unit aUnitInvert(const Unit& unit, StringView newname);
122
127AST_UTIL_API void aUnitFactorize(const Unit& unit, Unit& newUnit, double& scale);
128
132AST_UTIL_API void aUnitFactorize(Unit& unit, double& scale);
133
134
135
136
138class Unit
139{
140public:
141 #ifdef AST_BUILD_LIB_UTIL
142 friend Unit unit_multiply(const Unit& unit1, const Unit& unit2);
143 friend Unit unit_divide(const Unit& unit1, const Unit& unit2);
144 friend Unit unit_power(const Unit& unit, int exponent);
145 friend void unit_setname(Unit& unit, StringView name);
146 #endif
147
148 class UnitRep;
149 using UnitRepHandle = std::shared_ptr<UnitRep>;
150 using UnitRepPair = std::pair<UnitRepHandle, int>;
151 using SubUnitList = std::vector<UnitRepPair>;
152
153 using UnitRepHandleConst = std::shared_ptr<const UnitRep>;
154 using UnitRepPairConst = std::pair<UnitRepHandleConst, int>;
155 using SubUnitListConst = std::vector<UnitRepPairConst>;
156
159 class UnitRep{
160 public:
161 UnitRep(StringView name, double scale, Dimension dimension, const SubUnitListConst& subunits)
162 : name_{std::string(name)}
163 , scale_{scale}
164 , dimension_{dimension}
165 , subUnits_{subunits}
166 {}
167 std::string name_;
168 double scale_{1.0};
171 };
172public:
174 static Unit NaN()
175 {
176 return Unit("", 0.0, EDimension::eUnit);
177 }
178
180 static Unit None()
181 {
182 return Unit("", 1.0, EDimension::eUnit);
183 }
184
186 static Unit One()
187 {
188 // return Unit(aText("×1"), 1.0, EDimension::eUnit);
189 return Unit(aText("\u00D71"), 1.0, EDimension::eUnit);
190 }
191
195 AST_UTIL_API
196 static Unit Scale(double scale);
197
199 static Unit Percent()
200 {
201 return Unit("%", 0.01, EDimension::eUnit);
202 }
203
204
206 static Unit Second()
207 {
208 return Unit("s", 1.0, EDimension::eTime);
209 }
211 static Unit Minute()
212 {
213 return Unit("min", 60.0, EDimension::eTime);
214 }
216 static Unit Hour()
217 {
218 return Unit("h", 3600.0, EDimension::eTime);
219 }
221 static Unit Day()
222 {
223 return Unit("day", 86400.0, EDimension::eTime);
224 }
227 {
228 return Unit("ms", 0.001, EDimension::eTime);
229 }
230
231
232
234 static Unit Meter()
235 {
236 return Unit("m", 1.0, EDimension::eLength);
237 }
240 {
241 return Unit("km", 1000.0, EDimension::eLength);
242 }
245 {
246 return Unit("cm", 0.01, EDimension::eLength);
247 }
250 {
251 return Unit("dm", 0.1, EDimension::eLength);
252 }
253
256 {
257 return Unit("mm", 0.001, EDimension::eLength);
258 }
259
260
262 static Unit Inch()
263 {
264 return Unit("in", 0.0254, EDimension::eLength);
265 }
266
268 static Unit Foot()
269 {
270 return Unit("ft", 0.3048, EDimension::eLength);
271 }
272
274 static Unit Yard()
275 {
276 return Unit("yd", 0.9144, EDimension::eLength);
277 }
278
280 static Unit Mile()
281 {
282 return Unit("mi", 1609.344, EDimension::eLength);
283 }
284
286 static Unit Kilogram()
287 {
288 return Unit("kg", 1.0, EDimension::eMass);
289 }
291 static Unit Gram()
292 {
293 return Unit("g", 0.001, EDimension::eMass);
294 }
295
298 {
299 return Unit("mg", 0.000001, EDimension::eMass);
300 }
301
303 static Unit Pound()
304 {
305 return Unit("lb", 0.45359237, EDimension::eMass);
306 }
307
309 static Unit Radian()
310 {
311 return Unit("rad", 1.0, EDimension::eAngle);
312 }
313
315 static Unit Degree()
316 {
317 // return Unit("°", 0.017453292519943295, EDimension::eAngle);
318 return Unit(aText("\u00B0"), kDegToRad, EDimension::eAngle);
319 }
320
323 {
324 return Unit(aText("\u2033"), kArcSecToRad, EDimension::eAngle);
325 }
326
328 static Unit Newton()
329 {
330 return Unit("N", 1.0, EDimension::eForce);
331 }
332
334 static Unit Ampere()
335 {
336 return Unit("A", 1.0, EDimension::eCurrent);
337 }
339 static Unit Kelvin()
340 {
341 return Unit("K", 1.0, EDimension::eTemperature);
342 }
344 static Unit Mole()
345 {
346 return Unit("mol", 1.0, EDimension::eAmount);
347 }
348
349 // 下面是组合单位
350
353 return Centimeter() / Second();
354 }
357 {
358 return Kilometer() / Hour();
359 }
362 {
363 return Meter() / Minute();
364 }
367 {
368 return Meter() * Meter();
369 }
372 {
373 return Kilometer() * Kilometer();
374 }
377 {
378 return Foot() * Foot();
379 }
380
383 {
384 return Yard() * Yard();
385 }
386
389 {
390 return Meter() * Meter() * Meter();
391 }
393 static Unit Liter()
394 {
395 return Unit("L", 0.001, EDimension::eVolume);
396 }
397
399 static Unit Pascal()
400 {
401 return Unit("Pa", 1.0, EDimension::ePressure);
402 }
403
404public:
406 Unit(): Unit{Unit::None()}{}
407
409 AST_UTIL_API
410 Unit(StringView name);
411
413 Unit(StringView name, double scale, Dimension dimension)
414 : rep_{std::make_shared<UnitRep>(name, scale, dimension, SubUnitListConst{})}
415 {}
416
417protected:
418 Unit(double scale, Dimension dimension, const SubUnitListConst& subunits)
419 : rep_{std::make_shared<UnitRep>("", scale, dimension, (subunits))}
420 {}
421
423 Unit(UnitRepHandle rep) : rep_(rep) {}
424
425public:
429 double fromSI(double value) const { return value / getScale(); }
430
434 double toSI(double value) const { return value * getScale(); }
435
439 double convertTo(double value, const Unit& unit) const { return value * (this->getScale() / unit.getScale()); }
440
444 double convertFrom(double value, const Unit& unit) const { return value * (unit.getScale() / this->getScale()); }
445
448 double getScale() const { return rep_->scale_; }
449
452 Dimension dimension() const { return rep_->dimension_; }
453
456 const std::string& name() const { return rep_->name_; }
457
460 bool isValid() const { return rep_->scale_ != 0.0; }
461
462public: // operators
463
466 Unit clone() const { return Unit(std::make_shared<UnitRep>(*rep_)); }
467
469 Unit invert() const { return aUnitInvert(*this); }
470
475 Unit multiply(StringView newname, const Unit& other) const
476 {
477 return aUnitMultiply(*this, other, newname);
478 }
479
484 Unit divide(StringView newname, const Unit& other) const
485 {
486 return aUnitDivide(*this, other, newname);
487 }
488
493 Unit scale(StringView newname, double factor) const
494 {
495 return Unit(newname, factor * rep_->scale_, rep_->dimension_);
496 }
497
502 Unit pow(StringView newname, int exponent) const
503 {
504 return aUnitPower(*this, exponent, newname);
505 }
506
510 Unit pow(int exponent) const
511 {
512 return aUnitPower(*this, exponent);
513 }
514
518 bool operator==(const Unit& other) const
519 {
520 if(rep_ == other.rep_)
521 return true;
522 // 单位缩放因子和量纲相同即可认为是相同单位
523 return rep_->scale_ == other.rep_->scale_ && rep_->dimension_ == other.rep_->dimension_;
524 }
525
529 bool operator!=(const Unit& other) const
530 {
531 return !(*this == other);
532 }
533
537 Unit operator*(const Unit& other) const
538 {
539 return aUnitMultiply(*this, other);
540 }
541
545 Unit operator/(const Unit& other) const
546 {
547 return aUnitDivide(*this, other);
548 }
552 Unit& operator*=(const Unit& other)
553 {
554 *this = *this * other;
555 return *this;
556 }
560 Unit& operator/=(const Unit& other)
561 {
562 *this = *this / other;
563 return *this;
564 }
565
566
567public:
568 UnitRepHandle rep_;
569};
570
571
575AST_NAMESPACE_END
Unit m3
立方米
定义 Unit.cpp:457
Unit mg
毫克
定义 Unit.cpp:444
Unit s
定义 Unit.cpp:435
Unit A
安培
定义 Unit.cpp:453
Unit minute
分钟,不要命名为min,容易与std::min冲突
定义 Unit.cpp:437
Unit cm
厘米
定义 Unit.cpp:424
Unit kg
千克
定义 Unit.cpp:442
Unit h
小时
定义 Unit.cpp:439
Unit arcsec
弧秒
定义 Unit.cpp:451
Unit mi
英里
定义 Unit.cpp:432
Unit day
定义 Unit.cpp:440
Unit lb
定义 Unit.cpp:445
Unit dm
分米
定义 Unit.cpp:425
Unit hour
小时
定义 Unit.cpp:438
Unit ms
毫秒
定义 Unit.cpp:436
Unit L
定义 Unit.cpp:458
Unit yd
定义 Unit.cpp:430
Unit m
定义 Unit.cpp:426
Unit deg
定义 Unit.cpp:449
Unit mm
毫米
定义 Unit.cpp:423
Unit sec
定义 Unit.cpp:434
Unit ft
英尺
定义 Unit.cpp:431
Unit none
无单位
定义 Unit.cpp:421
Unit Pa
帕斯卡
定义 Unit.cpp:460
Unit km
千米
定义 Unit.cpp:427
Unit rad
弧度
定义 Unit.cpp:450
Unit N
牛顿
定义 Unit.cpp:447
Unit m2
平方米
定义 Unit.cpp:455
Unit in
英寸
定义 Unit.cpp:429
Unit g
定义 Unit.cpp:443
Unit K
开氏温度
定义 Unit.cpp:462
量纲
定义 Dimension.hpp:355
单位表示
定义 Unit.hpp:159
SubUnitListConst subUnits_
子单位列表
定义 Unit.hpp:170
Dimension dimension_
量纲
定义 Unit.hpp:169
std::string name_
名称
定义 Unit.hpp:167
单位
定义 Unit.hpp:139
static Unit Newton()
牛顿单位
定义 Unit.hpp:328
std::vector< UnitRepPair > SubUnitList
单位表示子项列表
定义 Unit.hpp:151
Unit(StringView name, double scale, Dimension dimension)
新建单位
定义 Unit.hpp:413
static Unit Day()
天单位
定义 Unit.hpp:221
Unit multiply(StringView newname, const Unit &other) const
单位乘法
定义 Unit.hpp:475
std::vector< UnitRepPairConst > SubUnitListConst
单位表示子项列表(常量)
定义 Unit.hpp:155
static Unit Mole()
mole单位
定义 Unit.hpp:344
static Unit Meter()
米单位
定义 Unit.hpp:234
std::shared_ptr< UnitRep > UnitRepHandle
单位表示
定义 Unit.hpp:149
Unit scale(StringView newname, double factor) const
单位缩放
定义 Unit.hpp:493
double getScale() const
获取单位缩放因子
定义 Unit.hpp:448
static Unit Percent()
百分比单位
定义 Unit.hpp:199
static Unit Minute()
分钟单位
定义 Unit.hpp:211
bool operator!=(const Unit &other) const
单位不相等运算符
定义 Unit.hpp:529
Unit pow(StringView newname, int exponent) const
单位幂运算
定义 Unit.hpp:502
Unit divide(StringView newname, const Unit &other) const
单位除法
定义 Unit.hpp:484
Unit pow(int exponent) const
单位幂运算
定义 Unit.hpp:510
double toSI(double value) const
从该单位对应的值转换为国际单位制下的值
定义 Unit.hpp:434
Unit operator*(const Unit &other) const
单位乘法运算符
定义 Unit.hpp:537
static Unit Pascal()
帕斯卡单位(压力量纲)
定义 Unit.hpp:399
static Unit Milligram()
毫克单位
定义 Unit.hpp:297
bool operator==(const Unit &other) const
单位相等运算符
定义 Unit.hpp:518
static Unit Second()
秒单位
定义 Unit.hpp:206
static Unit None()
无单位
定义 Unit.hpp:180
static Unit CubicMeter()
立方米单位
定义 Unit.hpp:388
static Unit NaN()
无效单位
定义 Unit.hpp:174
static Unit SquareMeter()
平方米单位
定义 Unit.hpp:366
static Unit Yard()
码单位
定义 Unit.hpp:274
Dimension dimension() const
获取单位量纲
定义 Unit.hpp:452
static Unit Gram()
克单位
定义 Unit.hpp:291
Unit & operator*=(const Unit &other)
单位乘法赋值运算符
定义 Unit.hpp:552
static Unit Kilometer()
米单位
定义 Unit.hpp:239
Unit invert() const
单位倒数
定义 Unit.hpp:469
static Unit SquareYard()
平方码单位
定义 Unit.hpp:382
std::shared_ptr< const UnitRep > UnitRepHandleConst
单位表示句柄(常量)
定义 Unit.hpp:153
static Unit Centimeter()
厘米单位
定义 Unit.hpp:244
static Unit Liter()
升单位
定义 Unit.hpp:393
const std::string & name() const
获取单位名称
定义 Unit.hpp:456
double convertFrom(double value, const Unit &unit) const
转换为指定单位下的值
定义 Unit.hpp:444
static Unit Ampere()
安培单位
定义 Unit.hpp:334
Unit clone() const
单位克隆
定义 Unit.hpp:466
static Unit Kelvin()
开尔文单位
定义 Unit.hpp:339
static Unit Foot()
英尺单位
定义 Unit.hpp:268
static Unit Hour()
小时单位
定义 Unit.hpp:216
static Unit SquareFoot()
平方英尺单位
定义 Unit.hpp:376
Unit & operator/=(const Unit &other)
单位除法赋值运算符
定义 Unit.hpp:560
bool isValid() const
是否有效单位
定义 Unit.hpp:460
static Unit CentimeterPerSecond()
厘米每秒单位
定义 Unit.hpp:352
static Unit Millimeter()
毫米单位
定义 Unit.hpp:255
double fromSI(double value) const
从国际单位制转换为该单位对应的值
定义 Unit.hpp:429
static Unit Decimeter()
分米单位
定义 Unit.hpp:249
std::pair< UnitRepHandleConst, int > UnitRepPairConst
单位表示子项(常量)
定义 Unit.hpp:154
double convertTo(double value, const Unit &unit) const
转换为指定单位下的值
定义 Unit.hpp:439
static Unit Radian()
弧度单位
定义 Unit.hpp:309
static Unit ArcSecond()
弧秒单位
定义 Unit.hpp:322
static Unit Mile()
英里单位
定义 Unit.hpp:280
Unit(UnitRepHandle rep)
定义 Unit.hpp:423
Unit()
默认无单位
定义 Unit.hpp:406
static Unit Inch()
英寸单位
定义 Unit.hpp:262
static Unit Degree()
度单位
定义 Unit.hpp:315
Unit operator/(const Unit &other) const
单位除法运算符
定义 Unit.hpp:545
static Unit One()
无量纲单位
定义 Unit.hpp:186
static Unit Pound()
磅单位
定义 Unit.hpp:303
static Unit MeterPerMinute()
米每分钟单位
定义 Unit.hpp:361
static Unit Kilogram()
千克单位
定义 Unit.hpp:286
static Unit MilliSecond()
毫秒单位
定义 Unit.hpp:226
static Unit KilometerPerHour()
千米每秒单位
定义 Unit.hpp:356
static Unit SquareKilometer()
平方公里单位
定义 Unit.hpp:371
std::pair< UnitRepHandle, int > UnitRepPair
单位表示子项
定义 Unit.hpp:150
Unit aUnitMultiply(const Unit &unit1, const Unit &unit2)
单位乘法
定义 Unit.cpp:330
Unit aUnitPower(const Unit &unit, int exponent)
单位幂
定义 Unit.cpp:358
Unit aUnitInvert(const Unit &unit)
单位倒数
定义 Unit.cpp:373
void aUnitFactorize(const Unit &unit, Unit &newUnit, double &scale)
单位分解
定义 Unit.cpp:387
Unit aUnitDivide(const Unit &unit1, const Unit &unit2)
单位除法
定义 Unit.cpp:344
constexpr double kArcSecToRad
弧秒到弧度 kDegToRad/3600.0 度分秒的秒
定义 Constants.h:213
constexpr double kDegToRad
角度到弧度 kPI/180.0
定义 Constants.h:212
Unit unit_divide(const Unit &unit1, const Unit &unit2)
单位除法
定义 Unit.cpp:264
Unit unit_power(const Unit &unit, int exponent)
单位幂
定义 Unit.cpp:297
void unit_setname(Unit &unit, StringView name)
设置单位名称
定义 Unit.cpp:324
Unit unit_multiply(const Unit &unit1, const Unit &unit2)
单位乘法
定义 Unit.cpp:233