今天我要写一个二进制STL 文件解析器,从下面的代码开始:
import Data.Binary.Get
import Data.Word
import Control.Monad
import Data.Bits
import Data.Binary.IEEE754
data Vector3 = Vector3 { x :: Float, y :: Float, z :: Float }
data Triangle = Triangle { normal :: Vector3,
vertex1 :: Vector3,
vertex2 :: Vector3,
vertex3 :: Vector3,
attr :: Word16}
getVector3 :: Get Vector3
getVector3 = do
w1 <- getFloat32le
w2 <- getFloat32le
w3 <- getFloat32le
return $ Vector3 w1 w2 w3
getTriangle :: Get Triangle
getTriangle = do
n <- getVector3
v1 <- getVector3
v2 <- getVector3
v3 <- getVector3
a <- getWord16le
return $ Triangle n v1 v2 v3 a
stlBinary :: Get ([Triangle])
stlBinary = do
_ <- getBytes 80 --Ignore the 80 byte header
cnt <- getWord32be --Read the number of triangles
replicateM (fromIntegral cnt) getTriangle
GHC 编译器抱怨
Couldn't match type `binary-0.5.1.1:Data.Binary.Get.Get' with `Get'
Expected type: Get Float
Actual type: binary-0.5.1.1:Data.Binary.Get.Get Float
In a stmt of a 'do' block: w3 <- getFloat32le
In the expression:
do { w1 <- getFloat32le;
w2 <- getFloat32le;
w3 <- getFloat32le;
return $ Vector3 w1 w2 w3 }
In an equation for `getVector3':
getVector3
= do { w1 <- getFloat32le;
w2 <- getFloat32le;
w3 <- getFloat32le;
.... }
它看起来像Get
s fromData.Binary
和Data.Binary.IEEE754
冲突。那么如何解决这个问题呢?
更新
我实际上有一个解决方法——将实现嵌入getFloat32le
到我的代码中(只有 6 行)
getFloat32le :: Get Float
getFloat32le = fmap toFloat getWord32le
toFloat :: (Storable word, Storable float) => word -> float
toFloat word = unsafePerformIO $ alloca $ \buf -> do
poke (castPtr buf) word
peek buf
这可行,但我仍然想知道如何处理名称冲突,因为复制代码并不好玩。
更新
$ghc-pkg list binary
/var/lib/ghc/package.conf.d
binary-0.5.1.1
/home/joe/.ghc/x86_64-linux-7.6.3/package.conf.d
binary-0.7.2.0
binary-0.7.2.1
$ ghc-pkg unregister binary-0.5.1.1
ghc-pkg: unregistering binary-0.5.1.1 would break the following packages:
bin-package-db-0.0.0.0 ghc-7.6.3 buildwrapper-0.8.6 scion-browser-0.3.1
hoogle-4.2.32 shake-0.12 buildwrapper-0.8.2 dynamic-cabal-0.3.1
(use --force to override)
$ ghc-pkg unregister binary-0.7.2.0
ghc-pkg: unregistering binary-0.7.2.0 would break the following packages:
snap-0.13.2.5 pwstore-fast-2.4.1 SHA-1.6.4 Graphalyze-0.14.1.0
pandoc-1.12.4.2 zip-archive-0.2.3.1 (use --force to override)
$ ghc-pkg unregister binary-0.7.2.1
ghc-pkg: unregistering binary-0.7.2.1 would break the following packages:
data-binary-ieee754-0.4.4 (use --force to override)
我不敢注销binary-0.7.2.0
and binary-0.5.1.1
,所以我注销data-binary-ieee754
and binary-0.7.2.1
,重新安装data-binary-ieee754
,然后rebuild,好像问题解决了!
问题:
我还有2个不同binary
的s,但是为什么这次没有问题呢?