Why use fcl::continuousCollide in FCL to report an error? - visual-studio-2019

I wrote a simple example of continuous collision using vs2019, but I can't always compile.
Error information:
C2259 'fcl::InterpMotion<S>': cannot instantiate abstract class
C2259 'fcl::ScrewMotion<S>': cannot instantiate abstract class
auto g1 = std::make_shared<fcl::Boxf>(1, 2, 3);
auto t1 = fcl::Transform3f::Identity();
auto o1 = new fcl::CollisionObjectf(g1, t1);
auto t1_final = fcl::Transform3f::Identity();
t1_final.translation() = fcl::Vector3f(1, 0, 0);
auto g2 = std::make_shared<fcl::Conef>(1, 3);
auto t2 = fcl::Transform3f::Identity();
auto o2 = new fcl::CollisionObjectf(g2, t2);
auto t2_final = fcl::Transform3f::Identity();
t2_final.translation() = fcl::Vector3f(-1, 0, 0);
fcl::ContinuousCollisionRequestf request;
fcl::ContinuousCollisionResultf result;
std::cout << fcl::continuousCollide(o1, t1_final, o2, t2_final, request, result);
delete o1;
delete o2;

in
fcl/math/motion/interp_motion.h
fcl/math/motion/interp_motion-inl.h
fcl/math/motion/screw_motion.h
fcl/math/motion/screw_motion-inl.h
I had to change double to S.
template <typename S>
bool InterpMotion<S>::integrate(double dt) const
template <typename S>
bool InterpMotion<S>::integrate(S dt) const

Related

how to create multi dimension LIsts in dart Language?

How to create Multidimensional List in dart?. IN other languages we can use array for this But in dart
we use List . SO i find in google I could not found method for Create Multidimensional List in dart ?
Anyone Know create Multidimensional List in dart ?
There are multiple ways to accomplish that. The simplest solution is to create a list of lists like:
void main() {
// Create 5x5 list
List<List<int>> twoDimList = List.generate(5, (_) => List.filled(5, 0));
twoDimList[0][0] = 5;
print(twoDimList);
}
A more efficient way to do it is to use a single list and access it by using two coordinates like the following class I have used in a previous project:
class Grid<T> {
final int length, height;
final List<T> list;
Grid(this.length, this.height) : list = List(length * height);
T get(int x, int y) => list[_getPos(x, y)];
void set(int x, int y, T value) => list[_getPos(x, y)] = value;
int _getPos(int x, int y) => x + (y * length);
}

How to do BigInt arithmetic in Dart 2.x, specifically division?

Dart documentation says that BigInt division returns a value of type 'double'. This is a problem. To illustrate, here are two implementations of an algorithm involving division. The first is in Kotlin, the second is in Dart. The Dart version runs accurately for small numbers but loses precision for larger numbers.
Kotlin
import java.math.BigInteger
fun height(n: BigInteger, m: BigInteger): BigInteger {
var m1 = m
var s = BigInteger("1")
var b = BigInteger("1")
var ans = BigInteger("0")
var i = 0
while (i < n.toInt()) {
s *= m1--
s /= b++
ans += s
i++
}
return ans
}
Dart
BigInt height(int n, int m) {
var m1 = m; // new BigInt.from(m);
var s = 1.0; // new BigInt.from(1);
var b = 1.0; // new BigInt.from(1);
var ans = new BigInt.from(0);
var i = 0;
while (i < n) {
s *= m1--;
s /= b++;
ans += BigInt.from(s);
i++;
}
return ans;
}
As you can see from the commented out Dart code, I have tried various ways to use BigInt.
Here is an example input with answer. The erroneous Dart answer is given below.
height(13, 550),
equals(BigInt.parse('60113767426276772744951355')));
The erroneous Dart answer is --> 60113767426276764034189615
Can someone show me the best way to do the job in Dart v2.x?
The following code works.
BigInt height(int n, int m) {
var m1 = new BigInt.from(m);
var s = new BigInt.from(1);
var b = new BigInt.from(1);
var ans = new BigInt.from(0);
var i = 0;
while (i < n) {
s *= m1;
m1 -= new BigInt.from(1);
s = s ~/ b;
b += new BigInt.from(1);
ans += s;
i++;
}
return ans;
}
Changes:
x++ and x-- are equivalent to x = x + 1 and x = x - 1 but BigInt.+ and BigInt.- only accept BigInt values... so there's a compiler error.
BigInt./ returns a double and this is not what you want here. You need to use the BigInt.~/ operator instead.

Is there anything like a struct in dart?

In javascript it always bothered me people use objects as vectors like {x: 1, y: 2} instead of using an array [1,2]. Access time for the array is much faster than the object but accessing by index is more confusing especially if you need a large array. I know dart has fixed arrays but is there a way to name the offsets of an array like you would a struct or a tuple/record in another language? Define enum/constants maybe?
I'd want something like
List<int> myVector = new List([x,y]);
myVector.x = 5;
is there an equivalent or idiomatic way to do this?
That sounds like a class.
class MyVector {
int x;
int y;
MyVector(this.x, this.y);
}
There is no simpler and more efficient way to create a name-indexed structure at runtime. For simplicity you could usually use a Map, but it's not as efficient as a real class.
A class should be at least as efficient (time and memory) as a fixed length list, after all it doesn't have to do an index bounds check.
In Dart 3.0, the language will introduce records. At that point, you can use a record with named fields instead of creating a primitive class:
var myVector = (x: 42, y: 37);
print(myVector.x);
A record is unmodifiable, so you won't be able to update the values after it has been created.
For me, i see 2 way to do this. I will sort by best in my point of view
Class based method
Here, the approach is to encapsulate your need, in a dedicated object
Pros:
It's encapsultate
You can propose several way to access variable, depend of the need
You can extend functionality without break everything
I love it :p
Cons
More time spend to create class, etc.
Do you really need what i say in pros ?
Maybe weird for js people
example :
class Vector {
int x;
int y;
static final String X = "x";
static final String Y = "y";
Vector({this.x, this.y});
Vector.fromList(List<int> listOfCoor) {
this.x = listOfCoor[0];
this.y = listOfCoor[1];
}
// Here i use String, but you can use [int] an redefine static final member
int operator[](String coor) {
if (coor == "x") {
return this.x;
} else if (coor == "y") {
return this.y;
} else {
// Need to be change by a more adapt exception :)
throw new Exception("Wrong coor");
}
}
}
void main() {
Vector v = new Vector(x: 5, y: 42);
Vector v2 = new Vector.fromList([12, 24]);
print(v.x); // print 5
print(v["y"]); // print 42
print(v2.x); // print 12
print(v2[Vector.Y]); // print 24
}
Enum based method:
You can also defined a "enum" (actually not really implement but will be in the future version) that will contains "shortcut" to your value
Pros
More simple to implement
Is more like your example ;p
Cons
Less extendable
i think is not very pretty
Not OOP think
example:
class Vector {
static final int x = 0;
static final int y = 1;
}
void main() {
List<int> myVector = new List(2);
myVector[Vector.x] = 5;
myVector[Vector.y] = 42;
}
Make your choice ;p
This is only possible with a class in Dart.
There are some open feature requests at http://dartbug.com
introduce struct (lightweight class)
Give us a way to structure Bytedata
If you have reasonably big data structure, you can use "dart:typed_data" as a model and provide lightweight view for the stored data. This way the overhead should be minimal.
For example, if you need 4X4 matrix of Uint8 values:
import "dart:typed_data";
import "dart:collection";
import "package:range/range.dart";
class Model4X4Uint8 {
final Uint8List _data;
static const int objectLength = 4 * 4;
final Queue<int> _freeSlotIndexes;
Model4X4Uint8(int length): _data = new Uint8List((length) * objectLength),
_freeSlotIndexes = new Queue<int>.from(range(0, length));
int get slotsLeft => _freeSlotIndexes.length;
num operator [](int index) => _data[index];
operator []=(int index, int val) => _data[index] = val;
int reserveSlot() =>
slotsLeft > 0 ? _freeSlotIndexes.removeFirst() : throw ("full");
void delete(int index) => _freeSlotIndexes.addFirst(index);
}
class Matrix4X4Uint8 {
final int offset;
final Model4X4Uint8 model;
const Matrix4X4Uint8(this.model, this.offset);
num operator [](int index) => model[offset + index];
operator []=(int index, int val) => model[offset + index] = val;
void delete() => model.delete(offset);
}
void main() {
final Model4X4Uint8 data = new Model4X4Uint8(100);
final Matrix4X4Uint8 mat = new Matrix4X4Uint8(data, data.reserveSlot())
..[14] = 10
..[12] = 256; //overlow;
print("${mat[0]} ${mat[4]} ${mat[8]} ${mat[12]} \n"
"${mat[1]} ${mat[5]} ${mat[9]} ${mat[13]} \n"
"${mat[2]} ${mat[6]} ${mat[10]} ${mat[14]} \n"
"${mat[3]} ${mat[7]} ${mat[11]} ${mat[15]} \n");
mat.delete();
}
But this is very low level solution and can easily create sneaky bugs with memory management and overflows.
You could also use an extension on List to create aliases to specific indexes.
Although it will be difficult to set up mutually exclusive aliases, in some cases, it may be a simple solution.
import 'package:test/test.dart';
extension Coordinates<V> on List<V> {
V get x => this[0];
V get y => this[1];
V get z => this[2];
}
void main() {
test('access by property', () {
var position = [5, 4, -2];
expect(position.x, 5);
expect(position.y, 4);
expect(position.z, -2);
});
}
The Tuple package https://pub.dev/packages/tuple might be what you are looking for when a class is too heavy.
import 'package:tuple/tuple.dart';
const point = Tuple2<int, int>(1, 2);
print(point.item1); // print 1
print(point.item2); // print 2

An error appears when running exist quantifier and fixedpoint Z3 in C#

I tried to use ctx.mkExist in the fixedpoint, howwever, it occurs error said "contains recursive predicate", I don't know why? and How to use ctx.MkExists in fixedpoint?For example:
exist (lamda real) that lamb>=0 AND inv(c,i) AND phi(c+lamb,i) => phi(c,i)
using (Context ctx = new Context())
{
var s = ctx.MkFixedpoint();
IntSort B = ctx.IntSort;
BoolSort T = ctx.BoolSort;
RealSort R = ctx.RealSort;
FuncDecl phi = ctx.MkFuncDecl("phi", new Sort[] { R,B }, T);
s.RegisterRelation(phi);
FuncDecl Inv = ctx.MkFuncDecl("inv", new Sort[] { R, B }, T);
s.RegisterRelation(Inv);
RealExpr c= (RealExpr)ctx.MkBound(0, R);
IntExpr i = (IntExpr) ctx.MkBound(1, B);
Expr[] InvArg=new Expr[2];
InvArg[0] = ctx.MkConst("inv0" , Inv.Domain[0]);
InvArg[1] = ctx.MkConst("inv1", Inv.Domain[1]);
Expr invExpr = ctx.MkImplies(ctx.MkOr(
ctx.MkAnd(ctx.MkEq(InvArg[1], ctx.MkInt(0)), ctx.MkGe((RealExpr)InvArg[0], ctx.MkReal(0))),
ctx.MkAnd(ctx.MkEq(InvArg[1], ctx.MkInt(1)), ctx.MkGe((RealExpr)InvArg[0], ctx.MkReal(2)))
),
(BoolExpr)Inv[InvArg]);
Quantifier invQ = ctx.MkForall(InvArg, invExpr, 1);
s.AddRule(invQ);
RealExpr[] lamb = new RealExpr[1];
lamb[0] = ctx.MkRealConst("lamb");
Expr existExpr = ctx.MkAnd(
(BoolExpr)Inv[c,i],
(BoolExpr)phi[ctx.MkAdd(c,lamb[0]),i],
ctx.MkGe(lamb[0], ctx.MkReal(0)));
BoolExpr t= ctx.MkExists(lamb, existExpr, 1);
s.AddRule(ctx.MkImplies(t,(BoolExpr)phi[c,i]));
}
sometimes, there is an error said "AccessViolationException was unhandlered,Attempted to read or write protected memory. This is often an indication that other memory is corrupt." when running to ctx.MkExists()
The fixedpoint solver only supports universal quantifiers at the top-level.
You should rewrite the rule as follows:
s.AddRule(ctx.MkForall(lamb,
ctx.MkImplies((BoolExpr)existExpr,(BoolExpr)phi[c,i])));
Z3 should ideally not result in any access violation. This is typically indicating a bug.
I would really appreciate repros for such bugs when/if you encounter them.

how to print Rich text box contents on any device contenxt with proper formatting?

i would like to print the rich text box contents with formatting to any device context, for example I would like to print on panel or any other control which is associated to actual print device.
I am simulating print preview using panel by drawing some contents from a custom designed form, rich text content is one among the content of that form
is there any best solution to address this??
below is code that prints an rtf control's contents to the printer. i could be adapted to print to any old dc fairly easy. the language is powerbasic, but it could be easily translated in to C, pascal or whatever:
SUB PrintRichTextBox ( hWnd as LONG, hInst as LONG, rtfEdit as LONG, LM as Single, _
RM as Single, TM as Single, BM as Single )
'
' Purpose:
' Prints the contents of an RTF text box given it's handle, the
' calling program's handle(s), and the page margins.
'
' Parameters:
' hWnd = Parent window (used for print common dlg)
' hInst = Instance of calling program
' rtfEdit = Handle of rich edit control
' LM = Left Margin in inches
' RM = Right Margin in inches
' TM = Top Margin in inches
' BM = Bottom Margin in inches
'
Dim fr as FORMATRANGE
Dim rDocInfo as DOCINFO
Dim iTextOut as LONG
Dim iTextAmt as LONG
Dim pd as PRINTDLGAPI
Dim zString as ASCIIZ * 200
Dim iWidthTwips&
Dim iHeightTwips&
'- Setup the print common dialog
pd.lStructSize = len(pd)
pd.hwndOwner = hWnd
pd.hDevMode = %NULL
pd.hDevNames = %NULL
pd.nFromPage = 0
pd.nToPage = 0
pd.nMinPage = 0
pd.nMaxPage = 0
pd.nCopies = 0
pd.hInstance = hInst
pd.Flags = %PD_RETURNDC or %PD_NOPAGENUMS or %PD_PRINTSETUP
pd.lpfnSetupHook = %NULL
pd.lpPrintSetupTemplateName = %NULL
pd.lpfnPrintHook = %NULL
pd.lpPrintTemplateName = %NULL
if PrintDlg(pd) then
SetCursor LoadCursor( %NULL, BYVAL %IDC_WAIT )
'- Fill format range structure
'
' NOTE:
' This gave me fits. I was looking at the book from
' Microsoft Press called Programming the Windows 95
' Iterface. It said (via example) that the
' Rectagle was defined in Pixels. This didn't work right.
' The SDK, however, said the measurements needed to be
' in Twips! This seems to work fine.
'
'
fr.hdc = pd.hDC
fr.hdcTarget = pd.hDC
fr.chrg.cpMin = 0
fr.chrg.cpMax = -1
fr.rc.nTop = TM * 1440
fr.rcPage.nTop = fr.rc.nTop
fr.rc.nLeft = LM * 1440
fr.rcPage.nLeft = fr.rc.nLeft
'- Get page dimensions in Twips
iWidthTwips& = int((GetDeviceCaps(pd.hDC, %HORZRES) / GetDeviceCaps(pd.hDC, %LOGPIXELSX)) * 1440)
iHeightTwips& = int((GetDeviceCaps(pd.hDC, %VERTRES) / GetDeviceCaps(pd.hDC, %LOGPIXELSY)) * 1440)
fr.rc.nRight = iWidthTwips& - RM * 1440
fr.rcPage.nRight = fr.rc.nRight
fr.rc.nBottom = iHeightTwips& - BM * 1440
fr.rcPage.nBottom = fr.rc.nBottom
'- Fill rDocInfo structure
rDocInfo.cbSize = len(rDocInfo)
zString = "RTF Printer"
rDocInfo.lpszDocName = VARPTR(zString)
rDocInfo.lpszOutput = %NULL
'- Here we go
StartDoc pd.hDC, rDocInfo
StartPage pd.hDC
'- This does the printing. We send messages
' to the edit box telling it to format it's
' text to fit the Printer's DC.
'
iTextOut = 0
iTextAmt = SendMessage(rtfEdit, %WM_GETTEXTLENGTH, 0, 0)
do while iTextOut < iTextAmt
iTextOut = SendMessage(rtfEdit, %EM_FORMATRANGE, _
1, VARPTR(fr))
if iTextOut < iTextAmt then
EndPage pd.hDC
StartPage pd.hDC
fr.chrg.cpMin = iTextOut
fr.chrg.cpMax = -1
end if
loop
SendMessage rtfEdit, %EM_FORMATRANGE, 1, %NULL
'- Finish the printing.
EndPage pd.hDC
EndDoc pd.hDC
DeleteDC pd.hDC
SetCursor LoadCursor( %NULL, BYVAL %IDC_ARROW )
else
' MsgBox "Canceled !"
end if
END SUB
I have used the below extended control to achieve my RTF printing.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace RichTextBoxPrintCtrl
{
public class RichTextBoxPrintCtrl : RichTextBox
{
//Convert the unit used by the .NET framework (1/100 inch)
//and the unit used by Win32 API calls (twips 1/1440 inch)
private const double anInch = 14.4;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CHARRANGE
{
public int cpMin; //First character of range (0 for start of doc)
public int cpMax; //Last character of range (-1 for end of doc)
}
[StructLayout(LayoutKind.Sequential)]
private struct FORMATRANGE
{
public IntPtr hdc; //Actual DC to draw on
public IntPtr hdcTarget; //Target DC for determining text formatting
public RECT rc; //Region of the DC to draw to (in twips)
public RECT rcPage; //Region of the whole DC (page size) (in twips)
public CHARRANGE chrg; //Range of text to draw (see earlier declaration)
}
private const int WM_USER = 0x0400;
private const int EM_FORMATRANGE = WM_USER + 57;
[DllImport("USER32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
// Render the contents of the RichTextBox for printing
// Return the last character printed + 1 (printing start from this point for next page)
public int Print(int charFrom, int charTo, PrintPageEventArgs e)
{
//Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
//Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * anInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
rectPage.Left = (int)(e.PageBounds.Left * anInch);
rectPage.Right = (int)(e.PageBounds.Right * anInch);
IntPtr hdc = e.Graphics.GetHdc();
FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = charTo; //Indicate character from to character to
fmtRange.chrg.cpMin = charFrom;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectToPrint; //Indicate the area on page to print
fmtRange.rcPage = rectPage; //Indicate size of page
IntPtr res = IntPtr.Zero;
IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam = IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);
//Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam);
//Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc);
//Return last + 1 character printer
return res.ToInt32();
}
}
}

Resources