与框架无关的结构问题
这将是一个相当模糊的问题,但我想知道如何解决我面临的问题...
我目前有许多不同的项目,这些项目是我不久前编写的,它们都有一个类似的框架来处理空间分区和排序,但是每个项目都实现了自己的该框架版本。它们都是基于图形的应用程序,但具有以下实现:
- Silverlight
- Winforms (GDI)
- XNA
- DirectX
- OpenGL (TAO)
尽管该框架有 5 个不同版本,但它们都执行完全相同的操作,只是接受不同的对象。一个例子是 GDI 使用 System.Drawing.Point 来完成大多数与绘图相关的任务,而 XNA 使用 Vector2,但是它们都是表示 X/Y 的对象,与其他库相同,有些将 x/y 值存储为 int, float,double 但最终它们的核心代表相同的数据。
空间分区框架的主要工作是处理这些 2/3 维值并生成树或围绕这些数字执行其他基于排序的任务。
我目前正在尝试将我的许多分布式框架统一为一个集中式框架,这样它就可以有一个项目,并编写测试来证明一切正常。
所以我的第一个想法是创建一些不可知的对象来处理这个 X/Y 问题,例如自定义 Point2D、Point3D、四元数、矩形等。但是,然后我必须将它们转换回其本机版本进行渲染,如果我是在每个渲染周期对每个对象中的每个点都执行此操作,这听起来像是巨大的性能损失(特别是当该框架旨在尽可能提高性能时)。
我正在考虑使用接口来解决这个问题,并在实现部分内部包装本机对象,但我仍然不确定这是否是最好的方法,因为我从未真正做过太多基于跨平台的开发,所以从来没有必须解决这些问题。
无论如何,现在停止胡言乱语,任何建议都会很棒!
This is going to be quite a vague question but I am wondering how to get around the issue im facing...
I currently have many different projects that I have written a while ago that all have a similar framework to deal with spatial partitioning and sorting, however each project has had its own version of this framework implemented. They are all graphically based applications but have implementations in:
- Silverlight
- Winforms (GDI)
- XNA
- DirectX
- OpenGL (TAO)
Although there are 5 different versions of this framework they all do EXACTLY the same thing, just accept different objects. An example would be GDI uses System.Drawing.Point for most of its drawing related tasks whereas XNA uses Vector2 however they are both objects that represent an X/Y, same with the other libraries, some store the x/y values as int,float,double but ultimately at the heart they represent the same data.
The main job of the spatial partitioning framework is to work off these 2/3 dimensional values and generate trees or do other sorting based tasks around these numbers.
Im currently trying to unify alot of my distributed frameworks into a single centralised one so it can have a single project, with tests written to prove everything is working ok.
So my first thought was to create some agnostic objects to deal with this X/Y problem like a custom Point2D, Point3D, Quaternion, Rect etc. However then i would have to convert them back to their native version for rendering, and if i were to do that for each point in each object every render cycle then it sounds like a massive performance penalty (especially when this framework is meant to be as performant as possible).
I was thinking about having interfaces that would solve this problem and just wrap up the native object internally on the implementation part, but im still not sure if this is the best way to go, as ive never really done much cross platform based development so never had to solve these issues.
Anyway will stop waffling now, and any advice would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是实现您自己的数学类型并使用这些类型进行所有计算。我可能会使用 XNA 作为基础。不仅因为它们非常好,还因为您可以从 Mono.XNA、SilverSprite 或 XNATouch 获得它们的开源实现。 (当然还有 XNA 中的框架本身。)从而为您节省大量工作。
我看到的主要问题是不同的平台使用不同的类型。 GDI 使用整数,XNA、DirectX 和 OpenGL 使用单精度浮点数(通常),Silverlight 使用双精度浮点数。
如果不是这种情况,您可以将您的类型分配给布局相同的本机类型,并依靠 JIT 对其进行优化。
OpenGL、DirectX 和 XNA(不包括 SpriteBatch 之类的东西)的好处是,您基本上可以告诉 API 您的数据布局 - 并且您可以在内部使用您喜欢的任何内容。
对于 GDI 和 Silverlight,您可能无论如何都难以实现超高性能图形。所以我会承受转换为浮动并返回的打击。当您要显示数据时,只需编写简单的转换函数即可。与渲染相比,转换类型只占 CPU 时间的极小百分比。
(实际上,我已经为 Silverlight 完成了此操作 - 而且确实不值得担心。)
My recommendation would be to implement your own maths types and do all your calculations using those. I would probably use XNA's as a base. Not only because they're pretty good, but also because you can get open-source implementations of them from Mono.XNA, SilverSprite or XNATouch. (And of course the framework itself, in XNA.) Thus saving you a lot of work.
The major problem I can see is that your different platforms use different types. GDI uses integers, XNA, DirectX and OpenGL use single-precision floats (generally), and Silverlight uses double-precision floats.
If that wasn't the case you'd be able to just assign your type to the identically-laid-out native one and depend on the JIT to optimise it away.
The nice thing about OpenGL, DirectX and XNA (excluding things like SpriteBatch) is that you basically tell the API your data layout - and you can use whatever you like internally.
For GDI and Silverlight, you're probably going to struggle to do ultra-high-performance graphics anyway. So I would just take the hit of converting to float and back. Just write the simple conversion functions for when you go to display your data. Converting types will be an extremely tiny, minuscule percentage of your CPU time, compared to rendering.
(I've actually done this for Silverlight - and it really is not worth worrying about.)